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

发布于 2024-09-05 12:08:12 字数 1703 浏览 1 评论 0原文

在我的应用程序中,我在 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);
    }
}

In my app I bring up a context menu on long click in a ListActivity. One of the options "Priority" pops up an AlertDialog with 3 radio button choices. The problem is, it displays an empty dialog box without my 3 choices, or the message that I set. Here is my code..

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

If I replace the .setSingleChoiceItems with a positive and negative button instead, it displays the buttons and the message as expected. What am I doing wrong in setting up my list of radio buttons? Here is my calling code as well..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡看悲欢离合 2024-09-12 12:08:12

想通了!我在 singleChoiceItem 对话框上使用 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;

Figured it out! I was using builder.setMessage on a singleChoiceItem dialog instead of builder.setTitle. It seems that dialogs using radio button choices don't support setting a message, only a title. Seems strange that the method is provided though. Anyhow, here is the working code..

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文