2010-06-11 72 views
5

在我的應用程序中,我長時間在ListActivity中調出上下文菜單。其中一個選項「優先級」彈出AlertDialog 3個單選按鈕選項。問題是,它顯示一個沒有我3個選擇的空對話框,或者我設置的消息。這裏是我的代碼..Android AlertDialog未顯示單選按鈕或消息

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

回答

22

想通了!我在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; 
+2

謝謝!我只是把頭靠在牆上摸了一個小時,試圖弄清楚爲什麼我的警報對話不斷出現。 – seanalltogether 2011-11-09 23:45:55

+0

非常感謝。這太瘋狂了。這可以在某個地方記錄下來。 – njzk2 2012-03-27 15:34:41

+0

坦克!我瘋狂地尋找這個bug! – 2012-09-25 16:06:12