7

私のアプリでは、ListActivityを長押しするとコンテキストメニューが表示されます。オプション「優先度」の1つは、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);
    }
}
4

1 に答える 1

28

理解した!builder.setTitleの代わりにsingleChoiceItemダイアログでbuilder.setMessageを使用していました。ラジオボタンの選択肢を使用するダイアログは、メッセージの設定をサポートしておらず、タイトルのみをサポートしているようです。しかし、メソッドが提供されているのは奇妙に思えます。とにかく、ここに動作するコードがあります。

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;
于 2010-06-11T18:44:48.053 に答える