Это мой код. Что я могу изменить?
введите описание изображения здесь
введите описание изображения здесь
введите описание изображения здесь
введите здесь описание изображения
Фрагмент уведомления
public class NotificationsFragment extends Fragment {
private ArrayList<String> arrayList;
private ListView listView;
private RequestQueue queue;
private ArrayAdapter arrayAdapter;
private List<Earthquake> quakeList;
private int contentView;
private View view;
Я думаю, у меня проблема с этим списком и этой строкой кода: listView = getActivity (). findViewById (R.id.listview). Я пытаюсь исправить это, но ничего не получается.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_notifications);
quakeList = new ArrayList<>();
listView = getActivity().findViewById(R.id.listview);
queue = Volley.newRequestQueue(getActivity().getApplicationContext());
arrayList = new ArrayList<>();
getAllQuakes(Constants.URL);
}
Остальной код. Что я могу вернуть, чтобы показать список?
private void setContentView(int fragment_notifications) {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_notifications, container, false);
return view;
}
void getAllQuakes(String url) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Earthquake earthquake = new Earthquake();
Как я могу отправить с консоли в список? Стоит ли что-то менять?
try {
JSONArray features = response.getJSONArray("features");
for (int i = 0; i < Constants.LIMIT; i++) {
JSONObject properties = features.getJSONObject(i).getJSONObject("properties");
JSONObject geometry = features.getJSONObject(i).getJSONObject("geometry");
JSONArray coordinates = geometry.getJSONArray("coordinates");
double lon = coordinates.getDouble(0);
double lat = coordinates.getDouble(1);
//Log.d("Quake", lon + ", " + lat);
Log.d("Lat", properties.getString("place"));
earthquake.setPlace(properties.getString("place"));
earthquake.setType(properties.getString("type"));
earthquake.setTime(properties.getLong("time"));
earthquake.setMagnitude(properties.getDouble("mag"));
earthquake.setLon(lon);
earthquake.setLat(lat);
arrayList.add(earthquake.getPlace());
}
ArrayAdapter:
final ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(), "Clicked " + position, Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Clicked"+ position,Toast.LENGTH_LONG).show();
}
});
arrayAdapter.notifyDataSetChanged();