14

I have generic class

public abstract class BaseAdapter<T> extends RecyclerView.Adapter {
  private List<T> itemsList = new ArrayList<>();
  //other override methods

  @Override
    public long getItemId(int position) {
        return position;
    }
}

What is the correct way to implementing getItemId()? I think that return position like in many example is not correct.

Sky
  • 521
  • 1
  • 5
  • 14

3 Answers3

15
  1. Create a base interface that has a method that returns a type long eg.

    interface BaseInterface{
        long getId(); 
    }
    
  2. Change

    abstract class BaseAdapter<T> extends RecyclerView.Adapter 
    

    to

    abstract class BaseAdapter<T extends BaseInterface> extends RecyclerView.Adapter {
    

    Note: The signature changed to T extends BaseInterface

  3. Replace

    @Override
    public long getItemId(int position) {
        return position;
    }
    

    with

    @Override
    public long getItemId(int position) {
        return itemsList.get(position).getId();
    }
    
blizzard
  • 5,275
  • 2
  • 34
  • 48
Bubunyo Nyavor
  • 2,511
  • 24
  • 36
4

In the List you could only return the Id of the Item available at specific row as mentioned by Google documents:

getItemId

Get the row id associated with the specified position in the list.

But that's not the case with RecyclerView, in RecyclerView you have to ensure that you either return a unique Id for each Item or In case of no Stable Id you should return RecyclerView.NO_ID (-1). Please Refrain from returning values that are not stable. (An Stable value would be a unique value that does not change even if position of dataset changes)

Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
  • 1
    Exactly, the behavior of [getItemId](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemId(int)) in RecyclerView.Adapter differs from [getItemId](https://developer.android.com/reference/android/widget/ArrayAdapter.html#getItemId(int)) in ArrayAdapter. – Hasan El-Hefnawy Apr 07 '19 at 13:14
1

you should implement this methode: as you see you must tell the Adapter that you implemented that by setHasStableIds(true);

class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
MyAdapter() {
    setHasStableIds(true); // **very importent**
}

@Override
public long getItemId(int position) {
    // requires unique value, it means need to keep the same value
    // even if the item position has been changed.
    return mItems.get(position).getId();
}

}

Amir Hossein
  • 392
  • 3
  • 11
  • can you explain why this code gives a static value? to me it looks like `mItems.get(position)` returns an instance of your model class , which has a a function returning some integer id. why would your model return the same id again and again? If you are using dbs or network, then usually the `id` is unique and `getId()` returns a unique value everytime – ansh sachdeva Oct 22 '20 at 06:24
  • You're right. By "static" I mean unique value for each item. Not a same value. – Amir Hossein Oct 23 '20 at 10:09
  • Then please change the word `static` to `unique` , as this gives a totally different meaning, and newbies like me spend 10 minutes on this answer to get even more confused :P – ansh sachdeva Oct 25 '20 at 20:47