1

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;
}
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58

1 Answers1

0

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

Community
  • 1
  • 1
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • i want to get the id and sent it to delete method ! –  Sep 25 '16 at 11:19
  • So its simple, in your `onItemClick` method : `dbList.get(position).getId()` will give you the id of clicked position. – Yasin Kaçmaz Sep 25 '16 at 11:27
  • i do it ,, not working , on the deppugar give to me id always 0 –  Sep 25 '16 at 12:25
  • can you show me what you tried ? post your code and I will fix it for you – Yasin Kaçmaz Sep 25 '16 at 12:26
  • public void onItemLongClick(View view, int position) { ToDoModule a = new ToDoModule(); dbList.get(position).getId(); a.setX(dbList.get(position).getId()); FragmentManager fm = getSupportFragmentManager(); actionOption actionOption = new actionOption(); actionOption.show(fm,"fragment_edit_name"); } the varible x i create to send the postion from click to the delete method so , –  Sep 25 '16 at 12:31
  • public void onClick(View view) { ToDoModule to = new ToDoModule(); int id = to.getX(); db.deleteARow(id); Toast.makeText(getContext(), "delete", Toast.LENGTH_SHORT).show(); } –  Sep 25 '16 at 12:31
  • In your second comment you creating new ToDoModule instead of getting it. Then you created it the you getting is default integer value, I mean 0 because its an empty to do module. – Yasin Kaçmaz Sep 25 '16 at 12:35
  • i solve it based on your answer and change some code , thank you very much and i will accept the answer –  Sep 25 '16 at 13:18