0

So I have a custom list view which is populated with Name and phone number. I want to be able to long click on an item in the list view which will then popup a dialog which will allow me to edit the fields or delete the row. How can I do this? Currently I have the code below which just deletes the row if you longClick. I presume I have to create a Dialog class and then call it within the OnLongItemClickLister? I have no idea how to do this however, any help would be great ty.

      lvCustomList.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {

            ContactListItems contactListItems = (ContactListItems)arg0.getItemAtPosition(arg2);
            String id = contactListItems.getID();
            String delQuery = "DELETE FROM PHONE_CONTACTS WHERE id='"+id+"' ";
            sqlHandler.executeQuery(delQuery);
            showlist();

            return false;
        }
    });
Johnny
  • 1
  • 1
  • See following similar post [Android: using AlertDialog when an item from a listview is long pressed](http://stackoverflow.com/questions/6763766/android-using-alertdialog-when-an-item-from-a-listview-is-long-pressed) – ρяσѕρєя K Apr 21 '15 at 12:09
  • Try if it helps onItemLong click show a contextMenu to edit or delete next based on that show a custom alertDialog because you didn't specify which one you have to edit either phone no or name. – Raghavendra Apr 21 '15 at 12:13
  • Try this link http://stackoverflow.com/questions/17419357/how-to-open-menu-context-android-with-click-button-in-listview-adapter – Raghavendra Apr 21 '15 at 12:16
  • possible duplicate http://stackoverflow.com/questions/23195208/how-to-pop-up-a-dialog-to-confirm-delete-when-user-long-press-on-the-list-item – Rajen Raiyarela Apr 21 '15 at 12:23
  • I want to be able to edit both phone and name in the dialog – Johnny Apr 21 '15 at 13:14
  • you can populate dialog which contains two edittexts and two buttons(update/delete). Once click on item, you load the relevant name and phone number on edittexts. If click on update , run update query with new values, else run a delete query – Heshan Sandeepa Apr 21 '15 at 17:22

1 Answers1

0

First you have to create new XML file at folder menu under the folder res at your project. Name it as main_popup_menu and add this code:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/delete"
          android:title="@string/delete"
          android:onClick="doDelete"/>
    </menu>

And then, add this to your onItemLongClickListener:

    PopupMenu p = new PopupMenu(ViewDiaryActivity.this, view);
    p.getMenuInflater().inflate(R.menu.main_popup_menu, p.getMenu());
    p.show();

I've tried it, and it showed the popup delete button. :D

frogatto
  • 28,539
  • 11
  • 83
  • 129
Siti Aisyah
  • 71
  • 1
  • 3