Remove all items from RecyclerView
This works great for me:
public void clear() {
int size = data.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
data.remove(0);
}
notifyItemRangeRemoved(0, size);
}
}
Source: https://github.com/mikepenz/LollipopShowcase/blob/master/app/src/main/java/com/mikepenz/lollipopshowcase/adapter/ApplicationAdapter.java
or:
public void clear() {
int size = data.size();
data.clear();
notifyItemRangeRemoved(0, size);
}
For you:
@Override
protected void onRestart() {
super.onRestart();
// first clear the recycler view so items are not populated twice
recyclerAdapter.clear();
// then reload the data
PostCall doPostCall = new PostCall(); // my AsyncTask...
doPostCall.execute();
}
Android RecyclerView addition & removal of items
I have done something similar.
In your MyAdapter
:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public CardView mCardView;
public TextView mTextViewTitle;
public TextView mTextViewContent;
public ImageView mImageViewContentPic;
public ImageView imgViewRemoveIcon;
public ViewHolder(View v) {
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextViewTitle = (TextView) v.findViewById(R.id.item_title);
mTextViewContent = (TextView) v.findViewById(R.id.item_content);
mImageViewContentPic = (ImageView) v.findViewById(R.id.item_content_pic);
//......
imgViewRemoveIcon = (ImageView) v.findViewById(R.id.remove_icon);
mTextViewContent.setOnClickListener(this);
imgViewRemoveIcon.setOnClickListener(this);
v.setOnClickListener(this);
mTextViewContent.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(view, getPosition());
}
return false;
}
});
}
@Override
public void onClick(View v) {
//Log.d("View: ", v.toString());
//Toast.makeText(v.getContext(), mTextViewTitle.getText() + " position = " + getPosition(), Toast.LENGTH_SHORT).show();
if(v.equals(imgViewRemoveIcon)){
removeAt(getPosition());
}else if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public void removeAt(int position) {
mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataSet.size());
}
Edit:
getPosition()
is deprecated now, use getAdapterPosition()
instead.
How to remove items from recyclerview adapter
Try this would work notifyItemRemoved(position)
holder.canBtn.setOnClickListener(v -> {
cancelConfirmation(position);
});
Then pass as parameter of the current position as below cancelConfirmation(int position)
and finally when the response is successful remove the item as below
if (response.isSuccessful()) {
mData.remove(position)
notifyItemRemoved(position);
}
Take a look at the official documentation
How to clear/delete/remove my RecyclerView in Android?
Simply, call recyclerView.setAdapter(null);
If you can show me your adapter code, I can provide better method.
Deletion of multiple items(Including last item) from recyclerview list It throws error IndexOutOfBound
Try this
Output
We can achieve multiple way
Declare a list in your adapter
private var mSelectedItems = arrayListOf<Int>()
On your view holder chosen index add to the list
holder.itemView.setOnLongClickListener(View.OnLongClickListener {
// Selected item index
mSelectedItems.add(layoutPosition)
}
Your delete function like this
//Single or multiple call this same function
fun removeSelectedItems(){
mSelectedItems.sortedDescending()
mSelectedItems.forEach{itemIndex ->
YOUR_ITEM_LIST.removeAt(itemIndex)
notifyItemRemoved(itemIndex)
}
mSelectedItems.clear()
}
OR
fun removeSelectedItems(){
YOUR_ITEM_LIST.removeAll(mSelectedItems)
notifyDataSetChanged()
mSelectedItems.clear()
}
OR
If you've bulk record go with Diffutil...
Here is the example
https://developer.android.com/reference/androidx/recyclerview/widget/ListAdapter
You can use normal recyclerView adapter as well.
Deleting multiple (50+) items from recycler view is throwing exception (Stacktrace attached)
IndexOutOfBoundsException
is thrown if you run out of "The Bounds" of whatever you iterating, To fix in recyclerview you need to implement a mechanism to shift the items position up whenever you delete, so it will still be in "The bounds".
Use notifyItemRangeChanged(position, getItemCount());
From documentation: "You should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use RecyclerView.ViewHolder.getAdapterPosition()
which will have the updated adapter position."
How to delete items from Recycler View from another class?
public void deleteMethod(int position) {
cards.remove(position);
adapter.notifyItemRemoved(position)
}
As per my understanding, your code flow should be like below:
From one fragment you have open 1 bottom sheet that contains RV items. now when user clicks on RV item, You have open New fragment from "Card fragment" and dismissed bottom sheet. In new fragment you have passed bundle data. In card Fragment , you have delete button that will remove item from list of cards.
So From your card fragment using interface, you have to notify to your first fragment from where you have open Bottom sheet. In this fragment you have to remove that particular item from list for cards. ( you have to pass position from card fragment to first fragment to delete from list ).
Now your card list is updated. You can again open Bottom sheet with newly updated list. ( previously in onbindViewHolder, you have dismissed) .
Related Topics
Android Convert Px to Dp (Video Aspect Ratio)
Show Popup Above Map Marker in Mapview
Change Progressbar Color Through Code Only in Android
When Using Alertdialog.Builder with Edittext, The Soft Keyboard Doesn't Pop
Android How to Wait Until a Service Is Actually Connected
How to Set a Fragment Tag by Code
How to Detect If Changes Were Made in The Preferences
Starting Activity Through Notification: Avoiding Duplicate Activities
Speechrecognizer Causes Anr... I Need Help with Android Speech API
Isvalidfragment Android API 19
Android Word-Wrap Edittext Text
Ble Scan Is Not Working When Screen Is Off on Android 8.1.0
Set Animated .Gif as Background Android