Search code examples
androidadapter

RecyclerView and get ItemId


I am implementing a swipe to delete with a RecyclerView and an ItemTouchHelper. Within my onSwiped function, I call my service to delete the element with an API request, but for that I need the adapter to give me the id of the element. I overrided the getItemId() function of my Adapter

@Override
public long getItemId(int position) {
   return dataSet.get(position).getId();
}

However when I am using it in my onSwiped function

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
     AddBaseService addBaseService = AddBaseService.sharedInstance();
     long baseId = viewHolder.getItemId(viewHolder.getAdapterPosition());
     addBaseService.deleteBase(String.valueOf(baseId));

I got an error at the getAdapterPosition line saying

getItemId() in ViewHolder cannot be applied to (int)

although getAdapterPosition is supposed to return an int :/


Solution

  • EDIT:

    Here are two links that might help you solve your issue :


    OP used : adapter.getItemId(viewHolder.getAdapterPosition()); instead of viewHolder.getItemId() to solve his problem.