开发者

How to set DialogInterface.OnClickListener without AlertDialog.Builder?

开发者 https://www.devze.com 2023-04-06 13:36 出处:网络
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.

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消