how to get ID of the items in recyclerview

前端 未结 2 955
北荒
北荒 2021-01-06 17:18

i\'m using slide to delete from recyclerview but the problem comes when getAdapterPosition() doesn\'t match with the row_id of sql table. how can i get id from the adapter a

2条回答
  •  孤城傲影
    2021-01-06 17:58

    Use the getItemId method if you have overridden it in yr adapter like so

    @Override
         public long getItemId(int position) {
             return yrModel.getId();
           }
    
        yrViewHolder.idTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Long id = yrList.get(position).getId();
    
                Log.e(TAG, "List item ID: " + id);
            }
        });
    

    That way u get the item position and the ID using the getItemId method

提交回复
热议问题