AlertDialog's items not displayed

2019-03-09 20:57发布

I create an AlertDialog with an AlertDialog.Builder and set some items with setItems(). The dialog is shown but I cannot see any of the items. All I see is the message.

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
dialogBuilder.setMessage("Pick a color");
dialogBuilder.setItems(items, new DialogInterface.OnClickListener() {        
    public void onClick(DialogInterface dialog, int which) {
        // Do anything you want here
    }    
});

dialogBuilder.create().show();

If I set the PositiveButton, I can see that button just fine. I also tried setting MultiChoiceItems and SingleChoiceItems but neither of these work either.

6条回答
smile是对你的礼貌
2楼-- · 2019-03-09 21:07

try this

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
dialogBuilder.setTitle("Pick a color");
dialogBuilder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which) 
{
}
});
dialogBuilder.show();
查看更多
再贱就再见
3楼-- · 2019-03-09 21:11

Use Below Code:-

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
dialogBuilder.setTitle("Pick a color");
dialogBuilder.setItems(items, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        // Do anything you want here
    }

});
dialogBuilder.create().show();
查看更多
做个烂人
4楼-- · 2019-03-09 21:15

Use setTitle instead of setMessage which sets message body and overrides the items list.

查看更多
三岁会撩人
5楼-- · 2019-03-09 21:16

Try alertDialogBuilder.setCustomTitle(view)

查看更多
成全新的幸福
6楼-- · 2019-03-09 21:17

If you are using a resource string array you must include the resource packaging. context().getResources().getStringArray(R.array.items);

My list was not showing by using the R.array.items until i gave the pointer the context and resource packaging.

Good luck!

查看更多
Summer. ? 凉城
7楼-- · 2019-03-09 21:18

Why don't you go for setTitle instead of the setMessage? Try with setTitle("Pick a color").

I hope it will help you.

查看更多
登录 后发表回答