-1

I am struggling to find a Android official document for updating recycler view/dynamic list items upon a delete item operation. This for Java primarily, or Kotlin if applicable.

This is the only doc section I found and the one listed below it. The examples appear to lack this info too.

https://developer.android.com/develop/ui/views/layout/recyclerview#additional-resources

I know how to do it from past experience and other guides, but I'm trying to share an official reference from Android with a student of mine.

All of the docs I have seen just demonstrate adding items to a list but not deleting them.

There also appear to be no docs that show refreshing the list after an item has been deleted/added (i.e. notifyDataSetChanged and related functions)

Where are the official docs?

Drew Gallagher
  • 746
  • 2
  • 9
  • 18

1 Answers1

0

Suppose you want to delete an item from the list, you have to specify some things before proceed to delete it.

  1. Define the position of the item you want to delete. You can achieve it using getAdapterPosition() method.

  2. Pass this position to you delete function(User defined function).

    deleteItem(int position){ listItem.remove(position); notifyItemRemoved(position); }

  3. The above code will simply remove the item from your array list and then notify the adapter that particular item from particular position have been removed so rearrange the RecyclerView accordingly.

You can refer this answer notifyItemRemoved()