Click here to Skip to main content
16,017,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a notepad application that when you long click on a note in a list activity it will create a dialog that will ask you if you want to delete the note, and when you click yes it will delete the note. But for some reason this doesn't work and it is not becuase the listview does not refresh.

LongItemClick Listener:
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Delete...");
    alertDialog.setMessage("Are you sure you want to delete this note?");
    alertDialog.setCancelable(false);

    final int p = position;

    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            helper.delete(p);
        }
    });

    alertDialog.setNegativeButton("No", null);
    alertDialog.show();

    return true;
}


Delete row method:
C#
public void delete(int id) {
    getWritableDatabase().delete("notes", "_ID="+Integer.toString(id), null);
}
Posted

1 solution

Assuming you are using a CurstorAdapter, you will need to use the id, not the position for deleting. The position, is just the order in the list, it could potentially be equal to the id, but isn't always and to assume so, it could cause data loss.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900