Reputation: 661
I am trying to remove item from list and notifyItemRemoved()
method it is not working it gives array index out of bound exception by removing last item and if I remove item from middle its also remove very next item to it but if put the code outside the thread its working fine
there is my code
Runnable runnable = new Runnable() {
@Override
public void run() {
int progress = 0;
ArrayList<MediaFileObject> selection = localDatabase.getAllMediaObjectsOfID(list.get(selectedAlbum).getId());
for (MediaFileObject obj : selection) {
if (searializeableTasks.isCancelled())
break;
localDatabase.moveMediaObjectToTrash(obj);
progress++;
if (searializeableTasks.isCancelled())
sendProgressToHandle(-101);
else
sendProgressToHandle(progress);
}
if (!searializeableTasks.isCancelled())
localDatabase.deleteAlbum(list.get(selectedAlbum).getId());
Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
list.remove(selectedAlbum);
savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
}
});
}
};
searializeableTasks.startTasks(localDatabase.getAllMediaObjectsOfID(list.get(selectedAlbum).getId()).size(), runnable);
but if put all this code inside main thread(Class) its working fine as expected I am putting my code here I want to show my custom progress dialog there for after completion I want to update my RecyclerView
can someone please help me. What is wrong in my code? or Why it is not working in separate thread?
Upvotes: 2
Views: 1372
Reputation: 1343
use only
notifyDataSetChanged();
It will work.If you use notifyItemRemoved()
after remove()
then it would again try to remove next position , so Exception
arises.
Upvotes: -1
Reputation: 21073
Call notify on main thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
}
});
Or
yourrecyclerView.post(new Runnable() {
@Override
public void run() {
savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
}
});
Upvotes: 2