Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add onlongclick listener to an alertdialog

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();
}
like image 466
wallace.d Avatar asked Feb 04 '12 23:02

wallace.d


People also ask

How to use alertdialog in Android with onclicklistener?

Instantiate AlertDialog.Builder to set title, icon button etc. These act as OK, Cancel, Yes, No etc button and on click of which an action is performed. To perform it DialogInterface.OnClickListener provides onClick () method which is implemented. Here we will also see how to use different themes provided by android.

How to create alert dialog in Android Studio?

Step 1: Create a new project and name it AlertDialogExample. Step 2: Open res -> layout -> activity_main. xml (or) main. xml and add following code: Here we add the button UI on click of which alert dialog will appear. We also used textview for asking user to click on button.

How to set the icon on alert dialog box in Java?

setIcon () method is use to set the icon on Alert dialog box. Then we add the two Button, setPositiveButton and setNegativeButton to our Alert Dialog Box as shown below. Step 1: Create a new project. After that, you have java and XML file.

How to create alert dialog with title message and one button?

To perform it DialogInterface.OnClickListener provides onClick () method which is implemented. Here we will also see how to use different themes provided by android. android.app.AlertDialog is used to create alert dialog with title, message and one or two buttons.


1 Answers

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();

}

like image 164
tjk213 Avatar answered Sep 27 '22 21:09

tjk213