Add onlongclick listener to alertdialog

I have an AlertDialog in android that contains a list of buddies from sqlite. When I click on the name of the interlocutor in the list, this buddy is called. What I want to do is add a longclicklistener to the list so that I get an invitation to remove friends from the list. I am having problems with onlclick and onlongclick working on the same element. Can someone give me a pointer here. I have been working with Android for several months. Thanks for any help!

private void displayBuddyList(String region) { final String region2 = region; Context context = getApplicationContext(); dh = new DataBaseHelper(context); List<String> bnames = dh.selectBuddies(); Log.d(TAG, "Buddy Names: " +bnames); final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]); // final CharSequence[] items = {"Mark", "Vikrant", "Olle,"Jane","Dan"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select a Buddy"); builder.setItems(buds, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int item) { // showShortToast("Clicked on:"+buddy[item]); String ptcode = buds[item].toString();; if (region2 == "A") { callbuddy(ptcode,region2); } else if (region2 == "E") { callbuddy(ptcode,region2); } else if (region2 == "P") { callbuddy(ptcode,region2); } else { showShortToast("We have a bug"); } return; } }); builder.create().show(); } 
+4
source share
1 answer

One way to add OnLongClickListener is to override the OnShowListener dialog and install OnItemLongClickListener from the onShow dialog box (dialog box dialog). Try:

 private void displayBuddyList(String region) { final String region2 = region; Context context = getApplicationContext(); dh = new DataBaseHelper(context); List<String> bnames = dh.selectBuddies(); Log.d(TAG, "Buddy Names: " +bnames); final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]); // final CharSequence[] items = {"Mark", "Vikrant", "Olle,"Jane","Dan"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select a Buddy"); builder.setItems(buds, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int item) { // showShortToast("Clicked on:"+buddy[item]); String ptcode = buds[item].toString();; if (region2 == "A") { callbuddy(ptcode,region2); } else if (region2 == "E") { callbuddy(ptcode,region2); } else if (region2 == "P") { callbuddy(ptcode,region2); } else { showShortToast("We have a bug"); } return; } }); final AlertDialog ad = builder.create(); //don't show dialog yet ad.setOnShowListener(new OnShowListener() { @Override public void onShow(DialogInterface dialog) { ListView lv = ad.getListView(); //this is a ListView with your "buds" in it lv.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Log.d("Long Click!","List Item #"+position+"was long clicked"); return true; } }); } }); ad.show(); 

}

+9
source

Source: https://habr.com/ru/post/1394816/


All Articles