My list from JSON not showing on listview in mobile application, why?

Viewed 78

This is my code. What can I change?

Notification fragment

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;

I think I have problem with this listview and this line of code: listView = getActivity().findViewById(R.id.listview) . I try to fix this but no success.

    @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);

    }

The rest of the code.What can i return to show list?

    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();

How can I send from console to listview? Should I change something?

                                            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();
2 Answers

you know the life cycle of fragment is differnt with activity..

so in onCreate fragmnet dont need do any thing: write your code in onViewCreated()

it is work for me :

public class BlankFragment extends Fragment {

ListView listView;
ArrayList<String> arrayList;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    listView=getActivity().findViewById(R.id.list);
    arrayList=new ArrayList<>();
    getAllQuakes("");

}

void getAllQuakes(String url) {

    for(int i=0;i<10;i++){

        arrayList.add("vaa"+i);
    }



    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();

        }
    });
    adapter.notifyDataSetChanged();
}

}

Just change listView.setAdapter(arrayAdapter) to listView.setAdapter(adapter).

Related