Kulwinder Singh Rahal
Kulwinder Singh Rahal

Reputation: 519

Recyclerview not updating rows correctly when removing data from arraylist?

my RecyclerView contains Text and Delete Button when delete is clicked specific row will be deleted from ArrayList

                pending.remove(p);// pending is arraylist

after this updating RecyclerView by calling this function which is inside Adapter

swap(pending);

public void swap(ArrayList<DownloadingActivity.scheduleListType> newList) { if (pendingList != null) { pendingList.clear(); pendingList.addAll(newList); } else { pendingList = newList; } notifyDataSetChanged(); }

but the problem is that correct item deleted successfully from ArrayList pending but on RecyclerView wrong update is displayed.

if i delete second item from array list pending but in RecyclerView updated deleted item is last when activity is reloaded then values in RecyclerView are showing correctly

searched lot of about it but did not found any way to solve

Upvotes: 1

Views: 898

Answers (1)

Multidots Solutions
Multidots Solutions

Reputation: 591

Instaead of using swap() to delete the item from your array list and adding them back into diffrent array list, I will suggest that you use notifyItemRemoved in your adapter. So, your adapter will look like below.

RecyclerViewAdapter.java :

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    DownloadingActivity.scheduleListType> p = pendingList.get(position);
     //....
     //....

    holder.deleteBnt.setOnClickListner(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           pendingList.remove(p);
           notifyItemRemoved(position)
       }
    })
}

Upvotes: 2

Related Questions