Add onlongclick listener to an alertdialog
Asked Answered
H

1

5

I have an AlertDialog in android that contains a list of buddies from sqlite. When I click on the buddy name in the list, that buddy is called. What I want to do is add a longclicklistener to the list as well so I can be prompted to delete the buddies in the list. I am having trouble having onlclick and onlongclick work on the same element. Can someone give me a pointer here. I have been working with android for a few 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();
}
Herron answered 4/2, 2012 at 23:39 Comment(0)
G
11

one way to add an OnLongClickListener is by overriding the dialog's OnShowListener and setting an OnItemLongClickListener from within the onShow(DialogInterface dialog) method. Give this a 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();

}

Glynis answered 4/1, 2013 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.