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
).
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
remove an item from one recyclerview and add it to another
well, nobody answered me again and I found the solution myself:
I added both Arraylists in the adapter and exchanged datas within the adapter itself.
adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {
private ArrayList<TaskModel> tasks;
private ArrayList<TaskModel> tasks2;
private Context context;
public RecyclerViewAdapter(Context context, ArrayList<TaskModel> list1, ArrayList<TaskModel> list2){
tasks = list1;
tasks2 = list2;
this.context = context;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.title.setText(tasks.get(position).getTitle());
holder.description.setText(tasks.get(position).getDescription());
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tasks2.add(tasks.get(position));
tasks.remove(position);
MainActivity.listView.getAdapter().notifyDataSetChanged();
MainActivity.finishedListView.getAdapter().notifyDataSetChanged();
}
});
}
my data return after remove item in recyclerview
You forgot to save list after removing item:
public void removeItem(int position) {
mExampleList.remove(position);
// saving the list to shared preference:
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences12", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("task list12", new Gson().toJson(mExampleList)).apply();
///////////// end of saving
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
}
Related Topics
Download a File Programmatically on Android
Android Blur View (Blur Background Behind the View)
Prevent Screen Rotation on Android
How to Stop Activity Resizing/Pushing Up on Keyboard Open in Dialog
How to Completely Disable Selinux in Android L in the Init.Rc File
How to Remove the Black Bar Appearing At the Bottom
Remove Scroll Bar Track from Scrollview in Android
Android - Edittext Not Showing Any Text
How to Give Pause or Gap Between Words in Tts in Android
Android - Transform Classes With Dex for Debug
How to Keep a Bottom Nav Bar from Being Pushed Up on Keyboard Shown
How to Transfer from One Fragment to Another in Android
How to Figure Out Which Sim Received Sms in Dual Sim Android Device
Link Should Be Open in Same Web View in Android
How to Emulate Gps Location in the Android Emulator
How to Fix Attempt to Invoke Interface Method on a Null Object Reference