简体   繁体   中英

How to remove item from Recycleview from activity

I have recycleview where I implemented the search features. and each item has onClick added which removes items from list by below code.

  holder.del_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int newPosition = holder.getAdapterPosition(); contactListFiltered.remove(newPosition); notifyItemRemoved(newPosition); notifyItemRangeChanged(newPosition, contactListFiltered.size()); }); 

It works fine with normal list but when any item is searched then on filtered results onclick listener deletes randomly may due to overlap of views. So I pass the onclick listener to activity with below code

  mSolved.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // send selected contact in callback listener.onSolved(contactListFiltered.get(getAdapterPosition())); } }); public interface FRoomAdapterListener { void onSolved(Districtpost contact); } 

and in activity I am using

  public void onDelete(Districtpost contact) { int newPosition = holder.getAdapterPosition(); contactListFiltered.remove(newPosition); notifyItemRemoved(newPosition); notifyItemRangeChanged(newPosition, contactListFiltered.size()); }); 

which is certain to throw errors. How can I solve this. Thanks in advance.

try this

public void onDelete(Districtpost contact) {
if(contactListFiltered != null && contactListFiltered.size > 0){
contactListFiltered.remove(contact);
notifydatasetchanged();
}});

Try this:

yourList.remove(position); //Remove item from list
notifyItemRemoved(position); //notify changes made to the adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM