Android Recyclerview Not Updating Items
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
publicvoidsetAdapter(ArrayList<DataObject> results){
mAdapter = newMyRecyclerViewAdapter(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 = newDataObject(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 heresetAdapter(results)
Solution 2:
Please remove notifyDataSetChanged from the below places.
makeJsonArrayRequest();
mAdapter = newMyRecyclerViewAdapter(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 = newRequestListener();
volley_queue.addRequestFinishedListener(listener);
RequestListner class
classRequestListenerimplementsRequestQueue.RequestFinishedListener {
@OverridepublicvoidonRequestFinished(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 = newDataObject(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 = newDataObject(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"