There is a listView . There is a database. There is an icon with a cross in paragraph. There is an alert that is called by a cross. Deletion occurs in the adapter class. In the class of activation, there is a listener of clicking on the item and it performs another task. The code in the adapter class.
@Override public void bindView(View view, final Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); holder.textView1.setText(cursor.getString(cursor.getColumnIndex(DatabaseHandler.COL_NAME_NOTE))); holder.textView2.setText(cursor.getString(cursor.getColumnIndex(DatabaseHandler.COL_NOTE))); holder.textView3.setText(cursor.getString(cursor.getColumnIndex(DatabaseHandler.COL_DATE))); holder.linearItem.setBackgroundColor(cursor.getInt(cursor.getColumnIndex(DatabaseHandler.COL_COLOR))); holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("TAG", "clicked in adapter"); final AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle("Удаление..."); alert.setMessage("Вы уверены?"); alert.setPositiveButton("Да", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { DatabaseHandler dh = new DatabaseHandler(context); dh.delNote(getItemId(...?...)); } }); } }); } @Override public long getItemId(int position) { Cursor cursor = getCursor(); cursor.moveToPosition(position); return cursor.getInt(cursor.getColumnIndex(BaseColumns._ID)); } To delete a specific entry, you need to pass the desired Id (position) to the parameter, how to get the desired position?
In the imageView picture with a cross.
Class removal method
public void delNote(int id){ getReadableDatabase().delete(TABLE_NAME, "_id = " +id, null); }
bindView, by passing the position number to it as a final parameter, after which it can be obtained in the delete method. I do not make out the answer, because It looks like a crutch, maybe someone has a more beautiful solution. - pavelfinal int positionto the parameter and the studio started saying that something was wrong - T. KudaibergenbindViewmethod, just how to modify it correctly, the studio swears, asks to re-implement it - T. Kudaibergen