2010-02-25 100 views
1

當我嘗試使用ArrayAdaptor設置警報以顯示一組 項目時,顯示列表,但項目的字符不可見。 如果選擇該項目,則字符可見。抓住我的頭爲什麼 。欣賞任何建議。以下是模擬器的代碼和 屏幕截圖。AlertDialog不顯示來自ListAdapter的項目隱藏選項

public class MessageTest extends Activity implements 
View.OnClickListener { 
     public final static String debugTag = "MessageDemo::"; 
     Button alert; 
     Button toast; 
     String[] items={"item1", "item2", "item3", "item4", "item5" }; 


     @Override 
     public void onCreate(Bundle icicle) { 
       super.onCreate(icicle); 


       setContentView(R.layout.message); 


       alert=(Button)findViewById(R.id.alert); 
       alert.setOnClickListener(this); 
     } 


     public void onClick(View view) { 
       if (view==alert) { 
         ArrayAdapter<String> aa = new ArrayAdapter<String>(this, 
             android.R.layout.simple_list_item_single_choice, items); 


         new AlertDialog.Builder(this) 
           .setTitle("MessageTest") 
           .setSingleChoiceItems(aa, 0, new DialogInterface.OnClickListener() 
{ 
             public void onClick(DialogInterface dlg, int which) { 
              Log.d(MessageDemo.debugTag, 
"DialogInterface.OnClickListener::onClick() is called -> which = 
"+which); 
             } 
           }) 
           .setIcon(android.R.drawable.ic_dialog_alert) 
           .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dlg, int sumthin) { 
              Log.d(MessageDemo.debugTag, "OK button is clicked -> sumthin 
= "+sumthin); 
             } 
           }) 
           .setNeutralButton("Close", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dlg, int sumthin) { 
              Log.d(MessageDemo.debugTag, "Close button is clicked -> 
sumthin = "+sumthin); 
               // do nothing -- it will close on its own 
             } 
           }) 
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
{ 
             public void onClick(DialogInterface dlg, int sumthin) { 
              Log.d(MessageDemo.debugTag, "Cancel button is clicked -> 
sumthin = "+sumthin); 
             } 
           }) 
           .show(); 
       } 
     } 
+0

我不確定AlertDialog背景是灰色還是ArrayAdapter的字符太灰色,所以你看不到它們! – tbruyelle 2010-02-25 18:48:00

回答

0

由於Quintin已經在評論中提到過,問題的原因可能是文本顏色和列表項的背景是相同的。爲您的列表項目使用另一個視圖模板,例如。 android.R.layout.select_dialog_item

builder.setAdapter(
    new ArrayAdapter<Object>(context, android.R.layout.select_dialog_item, my_array) 
    { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     {       
      View row;       
      if (null == convertView) 
      { 
       row = inflater.inflate(android.R.layout.select_dialog_item, null); 
      } 
      else 
      { 
       row = convertView; 
      }       
      TextView tv = (TextView) row.findViewById(android.R.id.text1); 
      tv.setText(getItem(position).toString()); 

      return row;           
     } 

    }, ... 

佈局吹氣可以抓住在上下文:

final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
9

我知道我有點晚在回答,但我有同樣的問題,我通過簡單地改變固定它

ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, items); 

ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice, items); 

我希望它可以幫助別人。

+0

工作,謝謝一堆! – 2011-02-24 19:22:31

+0

@布蘭登:作品! Thanx和+1 – barmaley 2011-07-21 12:40:20