Lord Flash Lord Flash - 2 months ago 6x
Android Question

Alertdialog multiple listeners

I created a Dialog which displays a list of items.

Now I need to also catch long-click events for editing purposes.

How could this be archieved?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final UserAdapter adapter = new UserAdapter(this);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
selecteduUser = (User) adapter.getItem(which);
if (selecteduUser != null) {


currently I am seeing two solution

Option 1:

create your own custom dialog. create a activity with list view ( or listactivity whatever you prefer) and set it's theme as dialog.


There you can easily handle list item click. Then while starting this custom dialog activity pass the list data you want to show.

Option 2:

set setOnShowListener in your alertdialog in your approach. let me edit from your last line of code

AlertDialog dialog = builder.create();

dialog.setOnShowListener(new OnShowListener() 
    public void onShow(DialogInterface dialog) 
        ListView list = ad.getListView();
        list.setOnItemLongClickListener(new OnItemLongClickListener() 
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
                // DO your task
                return true;