I have a RecyclerView on Android that downloads data with a PHP and Volley, and then displays a list of images with text. When I call another Activity that serves to filter searches for those objects in the DB, I press the apply flitro button, and return to the Main Activity again where the RecyclerView was. It is assumed that this should update and show only the new records with the filters applied, however, it shows as the first object the one that finds the filter applied, and then it continues showing the other objects that the application initially showed. I do not know if it's a problem updating the RecyclerView or not. This is the code of the method that fills the list with the filtered objects.
public static boolean filter=false;
private List<PlumImage> listPlums;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
listPlums = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);
//Toast.makeText(MainActivity.this, String.valueOf(filter), Toast.LENGTH_SHORT).show();
if (filter==false){ //If filter is not enabled
getData();
}
else{
//Toast.makeText(MainActivity.this, String.valueOf(FiltersActivity.CATEGORY), Toast.LENGTH_SHORT).show();
getDataFilter();
}
recyclerView.setOnScrollChangeListener(this);
adapter = new PlumAdapter(listPlums, this);
recyclerView.setAdapter(adapter);
}
private void getDataFilter() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServerFilter(requestCount));
//Incrementing the request counter
requestCount++;
}
private JsonArrayRequest getDataFromServerFilter(int requestCount) {
//Initializing ProgressBar
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//Displaying Progressbar
progressBar.setVisibility(View.VISIBLE);
setProgressBarIndeterminateVisibility(true);
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(AppConfig.FILTER_URL + String.valueOf(requestCount)+"&category="+FiltersActivity.CATEGORY,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseDataFilter(response);
//Hiding the progressbar
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(MainActivity.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
private void parseDataFilter(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
//Creating the plum object
PlumImage plum = new PlumImage();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the plum object
plum.setPhoto(json.getString(AppConfig.TAG_IMAGE_URL));
plum.setName(json.getString(AppConfig.TAG_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
//Adding the plum object to the list
listPlums.add(plum);
}
//Notifying the adapter that data has been added or changed
recyclerView.destroyDrawingCache();
adapter.notifyDataSetChanged();
}