Mr.Android Mr.Android - 2 months ago 24
Android Question

get item id when click into RecyclerView

I have an application with

RecyclerView
and
Sqlite
, I insert data to table by using
DialogFragment
, and when I click longclick in item on the
RecyclerView
I display another
Dialog
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);
db.close();
}


this is RecyclerView adapter:

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

holder.Title.setText(dbList.get(position).getActionTitle());
holder.Desc.setText(dbList.get(position).getActionDesc());
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
FragmentTransaction fragmentTransaction = ((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
actionOption actionOption = new actionOption();
actionOption.show(fragmentTransaction,"fragment_edit_name");
return true;
}
});}


and this is the delete button click

public void onClick(View view) {
ToDoModule to = new ToDoModule();
db.deleteARow(to.getId());
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));
model.setActionTitle(Title);
model.setActionDesc(Description);
model.setId(ID);
modelList.add(model);
}while (cursor.moveToNext());
}
return modelList;
}

Answer

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

recyclerView.addOnItemTouchListener(new RecyclerViewClickManager(context,
            recyclerView, new RecyclerViewClickManager.OnItemClickListener() {
        @Override
        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
        }

        @Override
        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;

    mRecyclerView=recyclerView;

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

        @Override
        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));
            }
        }
    });
}

@Override
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;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}

@Override
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

Comments