Android AlertDialog不显示单选按钮或消息

7

在我的应用中,当我长按ListActivity时,会弹出一个上下文菜单。其中一个选项“优先级”会弹出一个带有3个单选按钮选项的AlertDialog。问题是,它显示了一个空的对话框,没有我的3个选项或我设置的消息。这是我的代码...

protected Dialog onCreateDialog(int id) {
    AlertDialog dialog;
    switch(id) {
    case DIALOG_SAB_PRIORITY_ID:
        final CharSequence[] items = {"High", "Normal", "Low"};

        AlertDialog.Builder builder = new AlertDialog.Builder(SabMgmt.this);
        builder.setMessage("Select new priority")
               .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });

        dialog = builder.create();            
        break;
    default:
        dialog = null;
    }
    return dialog;
}

如果我用正面和负面按钮替换.setSingleChoiceItems,它会按预期显示按钮和消息。在设置我的单选按钮列表方面我做错了什么?这是我的调用代码。
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
    case R.id.sabdelete:
        // Correct position (-1) for 1 header
        final SabQueueItem qItem = (SabQueueItem) itla.getItem(info.position-1);
        SabNZBdUtils.deleteItem(qItem.getNzo_id());
        getQueue();
        ListView lv = getListView();
        View v = lv.findViewById(R.id.sablistheader);
        setHeader(v);
        itla.notifyDataSetChanged();
        return true;
    case R.id.sabpriority:
        showDialog(DIALOG_SAB_PRIORITY_ID);
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}
1个回答

28

搞定了!我在单选项对话框上使用了builder.setMessage而不是builder.setTitle。似乎使用单选按钮选择的对话框不支持设置消息,只支持标题。虽然提供了该方法似乎有点奇怪。无论如何,下面是可工作的代码...

protected Dialog onCreateDialog(int id) {
    AlertDialog dialog;
    switch(id) {
    case DIALOG_SAB_PRIORITY_ID:
        final CharSequence[] items = {"High", "Normal", "Low"};

        AlertDialog.Builder builder = new AlertDialog.Builder(SabMgmt.this);
        builder.setTitle("Select new priority")
               .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });

        dialog = builder.create();  
        break;
    default:
        dialog = null;
    }
    return dialog;

3
谢谢!我刚刚撞了一个小时的墙,试图弄清楚为什么我的警告对话框一直为空。 - seanalltogether
非常感谢。这太疯狂了。这应该在某个地方有记录。 - njzk2
谢谢!我为寻找这个错误疯狂了! - L. G.
1
这不是一个错误,而是一种特性 ;) 我同意它并不那么好,我理解为什么不这样做。 - Saren Inden
非常感谢您的帮助,感谢您分享您的知识。您是如何猜到的? - Akash kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接