2010-06-11 21 views
5

Trong ứng dụng của tôi, tôi đưa ra một trình đơn ngữ cảnh khi nhấp chuột dài trong một ListActivity. Một trong các tùy chọn "Ưu tiên" bật lên một AlertDialog với 3 lựa chọn nút radio. Vấn đề là, nó sẽ hiển thị một hộp thoại trống mà không có 3 lựa chọn của tôi, hoặc tin nhắn mà tôi đã đặt. Đây là mã của tôi ..Android AlertDialog không hiển thị các nút radio hoặc tin nhắn

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

Nếu tôi thay thế nút .setSingleChoiceMặt mặc định, nút này sẽ hiển thị các nút và thông báo như mong đợi. Tôi đang làm gì sai khi thiết lập danh sách các nút radio? Đây cũng là mã gọi điện của tôi ..

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

Trả lời

22

Đã tìm ra! Tôi đã sử dụng builder.setMessage trên một hộp thoại đơnChoiceItem thay vì builder.setTitle. Dường như các hộp thoại sử dụng các lựa chọn nút radio không hỗ trợ cài đặt tin nhắn, chỉ có tiêu đề. Có vẻ lạ là phương pháp này được cung cấp mặc dù. Nhưng dù sao, đây là mã đang hoạt động ..

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

Cảm ơn bạn! Tôi chỉ đập đầu vào tường trong một giờ cố gắng tìm ra lý do tại sao hộp thoại cảnh báo của tôi tiếp tục trống rỗng. – seanalltogether

+0

Cảm ơn rất nhiều. điều này là điên. và điều này có thể được ghi lại ở đâu đó. – njzk2

+0

Xe tăng! Tôi đã điên rồ tìm kiếm lỗi này! –

Các vấn đề liên quan