How to remove an item from recyclerView in android

In my application, I have to use recyclerView and remove some elements.
I want to remove some elements from recyclerview , and for this I write the code below in the Adapter :

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private Context context; private List<TvTonightResult> model; public MyAdapter (Context context, List<TvTonightResult> model) { this.context = context; this.model = model; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_tv_tonight, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(MyAdapter.ViewHolder holder, final int position) { holder.tvTonightTitle.setText(Html.fromHtml(model.get(position).getName())); Glide.with(context) .load(model.get(position).getImageUrl()) .placeholder(R.drawable.default_image) .override(600, 900) .diskCacheStrategy(DiskCacheStrategy.ALL) .listener(new RequestListener<String, GlideDrawable>() { @Override public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { return false; } }) .into(holder.tvTonightImage); long time = 5 * 1000; holder.tvTonightTimeCounter.start(time); holder.tvTonightTimeCounter.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() { @Override public void onEnd(CountdownView cv) { removeItem(position); } }); } @Override public int getItemCount() { return 4; } public class ViewHolder extends RecyclerView.ViewHolder { private ImageView tvTonightImage, tvTonightChannel; private TextView tvTonightTitle, tvTonightDate; private CountdownView tvTonightTimeCounter; public ViewHolder(View itemView) { super(itemView); tvTonightImage = (ImageView) itemView.findViewById(R.id.row_tvTonightImage); tvTonightChannel = (ImageView) itemView.findViewById(R.id.row_tvTonightChannelImage); tvTonightTitle = (TextView) itemView.findViewById(R.id.row_tvTonightTitle); tvTonightTimeCounter = (CountdownView) itemView.findViewById(R.id.row_tvTonightTime); } } private void removeItem(int position) { model.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position, model.size()); } } 

I want this run to be done, delete :

 holder.tvTonightTimeCounter.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() { @Override public void onEnd(CountdownView cv) { removeItem(position); } }); 

But show me this error :

 FATAL EXCEPTION: main Process: com.example.app, PID: 9711 java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at java.util.ArrayList.remove(ArrayList.java:477) at com.example.app.Adapters.TvTonightAdapter.removeItem(TvTonightAdapter.java:102) at com.example.app.Adapters.TvTonightAdapter.access$300(MyAdapter.java:29) at com.example.app.Adapters.TvTonightAdapter$2.onEnd(MyAdapter.java:74) at com.example.app.Utils.Componenets.CountDownTimer.CountdownView$1.onFinish(CountdownView.java:129) at com.example.app.Utils.Componenets.CountDownTimer.CustomCountDownTimer$1.handleMessage(CustomCountDownTimer.java:74) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6247) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

How can I fix this error and remove the item from recyclerview ?

+5
source share
5 answers

I think you should call remove(holder.getAdapterPosition()) instead of remove(position) , because when the holder’s countdown ends, it could change its position.

Suppose the following situation:

Holder 1 Β° β†’ Countdown 5 seconds

Holder 2ΒΊ β†’ 10 seconds countdown

Since the second holder ends after the first, remove(1) will be called after 10 seconds, but since the first holder has already been removed, the second position of the holder will be 0. Therefore, it should call remove(0) instead of remove(1) (which will raise IndexOutOfBoundsException )

+4
source

try

 int newPosition = holder.getAdapterPosition(); 

in your removeItem () method and drag the position using newPosition.

like

  private void removeItem(int position) { int newPosition = holder.getAdapterPosition(); model.remove(newPosition); notifyItemRemoved(newPosition); notifyItemRangeChanged(newPosition, model.size()); } 
+4
source

There is no need to delete elements, if you want to display only 4 elements, just change the counting method as shown below.

  @Override public int getItemCount() { return (model.size()>4)?4:model.size(); } 
0
source

If you want to display only four elements if there are more than four elements and the exact number of elements if there are less than four. Then you can use this:

 @Override public int getItemCount() { int count = 0; if (model != null && model.size() > 0) { int listSize = model.size(); if (listSize > 4) { count = 4; } else { count = listSize; } } return count; } 
0
source

Delete item in Arraylist in Android

 holder.mRemoveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Get the clicked item label String itemLabel = mDataSet.get(position); // Remove the item on remove/button click mDataSet.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position,mDataSet.size()); // Show the removed item label`enter code here` Toast.makeText(mContext,"Removed : " + itemLabel,Toast.LENGTH_SHORT).show(); } 
0
source

Source: https://habr.com/ru/post/1268557/


All Articles