0
votes

I want to create custom AlertDialog, but without AlertDialog.Builder. I set ListView as content view and wish to set DialogInterface.OnClickListener on its items.

Here is method onCreate() of my custom MyAlertDialog extends AlertDialog.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Context context = getContext();
    setTitle("Custom title");
    ListView listView = new ListView(context);
    listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, new String[] { "One", "Two" }));
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    setContentView(listView);
}

I had read docs about DialogInterface, Dialog and AlertDialog many times, but I didn't find the option like "AlertDialog.setOnClickListener()".

The solution must be without AlertDialog.Builder.

1
are you extending dialog or alertdialog? - Sherif elKhatib
AlertDialog. I've edited just now :) - QuickNick
ok why dont you use the OnItemClickListener ? - Sherif elKhatib
Well, I didn't think about it, thanks! Please, write it as an answer so I may check it :) - QuickNick

1 Answers

2
votes

Use OnItemClickListener instead.

If you want to use your dialog in different places, Add a function to your Dialog and try to make your whole code more reusable.

public void setOnItemClickListener(OnItemClickListener listener){
    listView.setOnItemClickListener(listener);
}