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开发者_如何学C);
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.
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);
}
精彩评论