Skip to content Skip to sidebar Skip to footer

Android RecyclerView Not Updating Items

I have a RecyclerView on my tabview fragment Activity. iam using arrayList to fill items on the recyclerview. Those JSON data are taken from this URL through volley library. Here

Solution 1:

I didn't view your codes each line by line but what I can suggest is refactor these codes as a method like here

public void setAdapter(ArrayList<DataObject> results){
    mAdapter = new MyRecyclerViewAdapter(results);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();
}

Call this in onCreateView once and again call this after retrieving the results that means inside onResponse method like here

  try {
        for (int i = 0; i < response.length(); i++) {
            JSONObject person = (JSONObject) response.get(i);
            String title = person.getString("title");
            String description = person.getString("description");
            DataObject obj = new DataObject(title, description);
            results.add(i, obj);
        }
      }
      catch (JSONException e) {
        e.printStackTrace();
        Toast.makeText(getContext(),
                "Error: " + e.getMessage(),
                Toast.LENGTH_LONG).show();
        Log.d(TAG, e.getMessage());
    }
  //Call method here
     setAdapter(results)

Solution 2:

Please remove notifyDataSetChanged from the below places.

            makeJsonArrayRequest();
            mAdapter = new MyRecyclerViewAdapter(results);
            mAdapter.notifyDataSetChanged();
            mRecyclerView.setAdapter(mAdapter);
           mAdapter.notifyDataSetChanged();

notifyDataSetChanged should be called when you have changed the data in your adapter and want the adapter to know that data has been changes. So in this case you will call it some data added to your results array list.

So once you add data to your results in makeJsonArrayRequest() call

          mAdapter.notifyDataSetChanged(); 

after the for loop.

Also in your adapter constructor make sure you are not creating a new array list and are reusing results as follows.

      ArrayList<DataObject> results;

      MyRecyclerViewAdapter(results){
             this.results = results;
      }

Let me know if this works. If it does not please post your adapter code so i can further debug.


Solution 3:

You should notify DatasetChanged in onRequestFinished method of volley.

in MainActivity.java

 RequestListener listener;
 listener = new RequestListener();
 volley_queue.addRequestFinishedListener(listener);

RequestListner class

 class RequestListener implements RequestQueue.RequestFinishedListener     {
    @Override
    public void onRequestFinished(Request request) {
         mAdapter.notifyDataSetChanged();
   }
}

Solution 4:

try {
     for (int i = 0; i < response.length(); i++) {        
        JSONObject person = (JSONObject) response.get(i);
        String title = person.getString("title");
        String description = person.getString("description");
        DataObject obj = new DataObject(title, description);
        results.add(i, obj);
        mAdapter.notifyItemInserted(i);
    }

Solution 5:

try 
{
    for (int i = 0; i < response.length(); i++) 
    {    
         JSONObject person = (JSONObject) response.get(i);    
         String title = person.getString("title");
         String description = person.getString("description");
         DataObject obj = new DataObject(title, description);
         results.add(i, obj);    
    }
} 
catch (JSONException e) 
{
    e.printStackTrace();
    Toast.makeText(getContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
    Log.d(TAG, e.getMessage());
}
mAdapter.notifyDataSetChanged();
hidepDialog();

Post a Comment for "Android RecyclerView Not Updating Items"