I have a spinner in my application that has a function call to get data from the server. This data is displayed in a list based on recyclerView and on the adapter. So it turns out that at different points of the spinner I need to display different data in the list. I understand that I need to first clear my arrayList and further notify my adapter. In appearance, everything is clear, and even it turns out to clear the array, but the data does not redraw and I cannot understand why. Here is a way:

recyclerView.setAdapter(null); recyclerView.setLayoutManager(null); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(myLayoutManager); myAdapter.notifyDataSetChanged(); 

by reference . But this method does not work. I made a check if the list is not empty when choosing the spinner element, then we clean it and then call the function to fill it with data. Here’s what it looks like in a spinner:

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { switch (position) { case 0: type = 1; ms.setJ_type(type); getL_jobs(offset, type); break; case 1: type = 0; ms.setJ_type(type); getL_jobs(offset, type); break; default: type = 1; ms.setJ_type(type); getL_jobs(offset, type); break; } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); 

and here is the function to get the data:

  public void getL_jobs(final int offset, final int type) { if (jobModelArrayList.isEmpty()) { Log.w("MY_TAG", "EMPTY"); } else { Log.w("MY_TAG", String.valueOf(jobModelArrayList.size())); j_adapter = new JobAdapter(jobModelArrayList, getApplicationContext()); recyclerView.setAdapter(null); recyclerView.setLayoutManager(null); recyclerView.setAdapter(j_adapter); recyclerView.setLayoutManager(mLayoutManager); j_adapter.notifyDataSetChanged(); } ... } 

Before I found the above method, I tried to just clean the list and make a notify adapter, but this did not help. I can not understand what I did wrong.

  • No need to reset the adapter - replace the array with the data and call notifyDataSetChanged. - Andrey Mihalev
  • what to replace it with? on new? - Andrew Goroshko

1 answer 1

Here is an option:

 public void getL_jobs(final int offset, final int type) { if (jobModelArrayList.isEmpty()) { Log.w("MY_TAG", "EMPTY"); } else { Log.w("MY_TAG", String.valueOf(jobModelArrayList.size())); //Нужно реализовать в адаптере получение списка данных j_adapter.addData(jobModelArrayList); //j_adapter = new JobAdapter(jobModelArrayList, getApplicationContext()); //recyclerView.setAdapter(null); //recyclerView.setLayoutManager(null); //recyclerView.setAdapter(j_adapter); //recyclerView.setLayoutManager(mLayoutManager); j_adapter.notifyDataSetChanged(); } ... } 

I immediately implemented the method in the adapter class:

  public void refreshData(List<MyObject> objects){ mList = objects; notifyDataSetChanged(); }