In RecyclerView there is a button, when I click on it, the current record, which I store in sqlite, should be deleted. Created a method that will delete the record itself:

 public void deleteFruit(Fruit fruit) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CONTACT_FRUIT, KEY_ID_FRUIT + " = ?", new String[]{String.valueOf(fruit.getId())}); db.close(); } 

But I don’t know how to transfer the parameter of the item that I want to delete.

And where is it better to call this method: in the main activation or in the adapter?

Adapter code:

  • Show the adapter code. - post_zeew
  • I updated the question - Fedia

1 answer 1

In the onClick(...) method, get the current object and pass it to the delete method:

 Fruit fruit = fruits.get(position); deleteFruit(fruit); 

Do not forget to delete the object itself from fruits and update the data:

 fruits.remove(postion); RecyclerAdapter.this.notifyItemRemoved(postion); 
  • dbHelperFruit = new DBHelperFruit (this); I have a problem with initialization, what do I need to pass to the parameter? - Fedia
  • @Fedia, From an architectural point of view, this is all wrong and wry. And so you can try to transfer v.getContext() . - post_zeew
  • But could not help with the architecture of the project? And even I myself understand that everything is crooked, but I do not know how differently. - Fedia
  • @Fedia, Regarding the right architecture is a very voluminous topic. Read books on programming for Android. - post_zeew