开发者

Android AlertDialog not showing radio buttons or message

开发者 https://www.devze.com 2023-01-03 08:39 出处:网络
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 w

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()开发者_Go百科);
        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);
    }
}


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

精彩评论

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