Mr.Android Mr.Android - 25 days ago 7x
Android Question

get item id when click into RecyclerView

I have an application with

, I insert data to table by using
, and when I click longclick in item on the
I display another
which have a delete button, but I can't catch the id of the item which I click on it, any idea ??

this is delete method :

public void deleteARow(int idA)
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TODO_TABLE, id + "=" + idA, null);

this is RecyclerView adapter:

public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, final int position) {
final ToDoModule appShowModule = dbList.get( position );

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
FragmentTransaction fragmentTransaction = ((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
actionOption actionOption = new actionOption();,"fragment_edit_name");
return true;

and this is the delete button click

public void onClick(View view) {
ToDoModule to = new ToDoModule();
Toast.makeText(getContext(), "delete", Toast.LENGTH_SHORT).show();

and this is the method that get data from db table

public List<ToDoModule> getDataFromDB(){
List<ToDoModule> modelList = new ArrayList<ToDoModule>();
String query = "select * from "+ TODO_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()){
do {
ToDoModule model = new ToDoModule();
String Title = cursor.getString(cursor.getColumnIndex(COLUMN_Title));
String Description = cursor.getString(cursor.getColumnIndex(COLUMN_Des));
int ID = (cursor.getColumnIndex(id));
}while (cursor.moveToNext());
return modelList;


I am using this way to detect click and longClick events :

recyclerView.addOnItemTouchListener(new RecyclerViewClickManager(context,
            recyclerView, new RecyclerViewClickManager.OnItemClickListener() {
        public void onItemClick(View view, int position) {
            //this is single click
            //if you want to get viewholder here :
            MyViewHolder viewHolder=(MyViewHolder) recyclerViewNotes.getChildViewHolder(view);
            //also you have position so you can get your item id by position

        public void onItemLongClick(View view, int position) {
            //this is long click

And the RecyclerViewClickManager class :

public class RecyclerViewClickManager implements RecyclerView.OnItemTouchListener {
//this is the click interface for both click and longClick
public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);

private OnItemClickListener mListener;

private GestureDetector mGestureDetector;

private RecyclerView mRecyclerView;

public RecyclerViewClickManager(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
    mListener = listener;


    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        public boolean onSingleTapUp(MotionEvent e) {
            return true;

        public void onLongPress(MotionEvent e) {
            View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
            //here if you want to get ViewHolder too :
            YourViewHolder viewholder=(YourViewHolder) recyclerView.findContainingViewHolder(childView);

            if (childView != null && mListener != null) {
                mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));

public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());

    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));

    return false;

public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {

public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

Also you can follow this link too : recycler-view-item-click-handler

Also this SO question is good for referencing : recyclerview-onclick