How to update RecyclerView Adapter Data?

前端 未结 13 2323
误落风尘
误落风尘 2020-11-22 03:48

Trying to figure out what is the issue with updating RecyclerView\'s Adapter.

After I get a new List of products, I tried to:

  1. Update t

相关标签:
13条回答
  • 2020-11-22 04:25

    If nothing mentioned in the above comments is working for you. It might mean the problem lies somewhere else.

    One place I found the solution was in the way I was setting the list to the adapter. In my activity the list was a instance variable and I was changing it directly when any data changed. Due to it being a reference variable there was something weird going on. So I changed the reference variable to a local one and used another variable to update data and then pass to addAll() function mentioned in above answers.

    0 讨论(0)
  • 2020-11-22 04:26

    you have 2 options to do this: refresh UI from the adapter:

    mAdapter.notifyDataSetChanged();
    

    or refresh it from recyclerView itself:

    recyclerView.invalidate();
    
    0 讨论(0)
  • 2020-11-22 04:27

    I've solved the same problem in a different way. I don't have data I waiting for it from the background thread so start with an emty list.

            mAdapter = new ModelAdapter(getContext(),new ArrayList<Model>());
       // then when i get data
    
            mAdapter.update(response.getModelList());
      //  and update is in my adapter
    
            public void update(ArrayList<Model> modelList){
                adapterModelList.clear(); 
                for (Product model: modelList) {
                    adapterModelList.add(model); 
                }
               mAdapter.notifyDataSetChanged();
            }
    

    That's it.

    0 讨论(0)
  • 2020-11-22 04:29

    Update Data of listview, gridview and recyclerview

    mAdapter.notifyDataSetChanged();
    

    or

    mAdapter.notifyItemRangeChanged(0, itemList.size());
    
    0 讨论(0)
  • 2020-11-22 04:33

    I'm working with RecyclerView and both the remove and the update work well.

    1) REMOVE: There are 4 steps to remove an item from a RecyclerView

    list.remove(position);
    recycler.removeViewAt(position);
    mAdapter.notifyItemRemoved(position);                 
    mAdapter.notifyItemRangeChanged(position, list.size());
    

    These line of codes work for me.

    2) UPDATE THE DATA: The only things I had to do is

    mAdapter.notifyDataSetChanged();
    

    You had to do all of this in the Actvity/Fragment code not in the RecyclerView Adapter code.

    0 讨论(0)
  • 2020-11-22 04:33

    This is what worked for me:

    recyclerView.setAdapter(new RecyclerViewAdapter(newList));
    recyclerView.invalidate();
    

    After creating a new adapter that contains the updated list (in my case it was a database converted into an ArrayList) and setting that as adapter, I tried recyclerView.invalidate() and it worked.

    0 讨论(0)
提交回复
热议问题