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