lundi 13 février 2017

Remove RecyclerView Item from Dialog not updating

Vote count: 0

I have a simple RecyclerView for displaying items. Currently, from the RecyclerView.Adapter, I can delete items successfully using the following.

private void removeItem(int pos) {    
    notifyItemRangeChanged(pos, getItemCount());

I call it from the onItemClick() function in the ViewHolder.The animations work, the view is updated, everything works. Pretty standard RecyclerView.

However, what I'd like to do is have the user verify the item deletion via a Dialog. Here's the basic setup for the Dialog (leaving out unnecessary code):


AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Delete this item?");

final int itemPos = pos;

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {


So, I'm just moving the method call into the onClickListener of the Dialog.

The problem I'm having is that when the RecyclerView animates away the removed item, it animates it back in the exact same position, and the list stays the same. Like it's still there.

But, if I scroll down I get a out of bounds error:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position

Which means it's not actually there, and when go back and come into the View again, it's gone. So, it seems like it's cached and not updating the adapter or dataset. I read that it needs called on the main thread, so I modified my method to this:

private void removeItem(int pos) {
final int itemPos = pos;

Handler handler = new Handler(Looper.getMainLooper());
Runnable runnable = new Runnable() {
    public void run() {
        notifyItemRangeChanged(itemPos, getItemCount());

It's still not working, and I'm at a loss. I suspect it's a thread issue, but not sure where to turn from here.

asked 57 secs ago

Let's block ads! (Why?)

Remove RecyclerView Item from Dialog not updating

Aucun commentaire:

Enregistrer un commentaire