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 item from recyclerView in android
I think you should call remove(holder.getAdapterPosition())
instead of remove(position)
because, when the holder's countdown finishes, it might have changed its position.
Suppose the following situation:
1º holder -> 5 seconds countdown
2º holder -> 10 seconds countdown
Since the second holder finishes after the first one, after 10 seconds, remove(1)
will be called but, since the first holder has already been removed, the second holder's position will be 0. Hence, it should call remove(0)
instead of remove(1)
(which will cause an IndexOutOfBoundsException
).
Remove certain items which are empty from RecyclerView list?
I suggest performing that removal logic in the constructor of the adapter rather than in onBind
. onBind
happens as the recycler view is finalising the details of each view holder immediately before it's shown to the user. You want to do as little as possible logic in here to keep the recycler view performant.
Inside the constructor (or even before the list is passed in) you should perform a loop and remove those items that don't meet the criteria before setting the instance variable.
It's been a long time since I wrote code in java and so I'd end up with unhelpful incorrect syntax if I tried to do it here.
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
items not removed correctly from RecyclerView
Corresponding to your code it's expected behaviour. So, your code fetches files data from device file system in onCreate()
method of your activity and creates a list of representation of the files.
When recordingArrayList.remove(position);
is invoked, it removes item from the representation list, but not file from file system.
When you call another activity and goes back, activity with the list is created again and onCreate()
is called again and your code fetches again all the files from device file system and shows it in a list.
Possible solution can be: remove file from file system, if you have read-write access to this folder or save a list in your app. For example, in Room database and add to your file representation field isDeleted
and use data from database to show in a list.
Related Topics
What's Java Hybrid - Applet + Application
How to Use Swingworker in Java
Execute Shell Command from Android
Differencebetween List and Arraylist
How to Check If a File Is Open by Another Process (Java/Linux)
Execute External Program Through Terminal in Java
How to Set Up Sublime Text 3 to Run and Compile Java on Linux
Eclipse Swt Browser Crash (Linux 64Bit)
Multiple Row Selection in Jtable
Java.Lang.Outofmemoryerror: Java Heap Space
Detecting a Long Press in Android
Limit Decimal Places in Android Edittext
Firebase No Properties to Serialize Found on Class
Why Does Tomcat Work with Port 8080 But Not 80