0

I have a list, I'd like to allow user to delete an item using long click and edit when click.

The problem is, if user would like to delete an item (long click on the item) my app will open the delete confirmation message and also the edit confirmation too.

Any ideas how to open only the delete (setOnItemLongClickListener) message when user long click a list item?

// DELETE
list.setOnItemLongClickListener((parent, view, position, arg3) -> {

    Contacts contacts = queue.get(position);

    AlertDialog.Builder adb=new AlertDialog.Builder(Read.this);
    adb.setTitle("Delete?");
    adb.setMessage("Are you sure you want to delete?");
    adb.setNegativeButton("Cancel", null);
    adb.setPositiveButton("Ok", (dialog, which) -> {

        deleteContact(Read.this, contacts.phone, contacts.name);

        mobileArray.remove(position);
        adapter.notifyDataSetChanged();

    });
    adb.show();

    return false;

});


// EDIT
list.setOnItemClickListener((parent, view, position, arg3) -> {

    Contacts contacts = queue.get(position);

    AlertDialog.Builder adb=new AlertDialog.Builder(Read.this);
    adb.setTitle("Edit?");
    adb.setMessage("Edit?");
    adb.setNegativeButton("Cancel", null);
    adb.setPositiveButton("Ok", (dialog, which) -> {

        Log.d("edit:", contacts.name + contacts.id + contacts.phone + contacts.email);

    });
    adb.show();


});
RGS
  • 4,062
  • 4
  • 31
  • 67
  • 2
    Just change the return to true ( source https://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener) – Ikazuchi Mar 09 '21 at 11:59
  • @Ikazuchi oh, it seems to work. Thanks for your answer. – RGS Mar 09 '21 at 12:16

1 Answers1

1

Although I don't think the approach in itself is that feasible, you should perhaps utilize Swipes to do multiple actions for better UX.

Anyway, a possible solution could be that you store private instance of both the Alert Dialogs like so:

private AlertDialog clickDiag;

private void function(){
   AlertDialog.Builder adb=new AlertDialog.Builder(Read.this);
   clickDiag = adb.show();
}

Now that you've got the instance stored, there's a useful function in Dialogs:

clickDiag.isShowing();

Which you could by now I think would understand that it can be used as a check to whether one or the other instance of a particular dialog is visible so don't show the other and vise-versa.

Dharman
  • 30,962
  • 25
  • 85
  • 135