当我尝试使用ArrayAdaptor设置警报以显示一组 项目,列表显示但项目的字符不可见。 如果选择了该项,则字符可见。搔抓 我的头脑为什么。感谢任何建议。下面是代码和 来自模拟器的屏幕截图。
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 :(得分: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);
我希望它有助于某人。
答案 1 :(得分: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;
}
}, ...
布局inflater可以在上下文中抓取:
final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);