AlertDialog doesn't display items from ListAdapter Hide options
When I try to set the Alert using ArrayAdaptor to display a set of items, the list is displayed but the items' characters are invisible. If the item is selected, then the characters are visible. Scratching my head on why. Appreciate any advice. Below is the code and the screenshot from the emulator.
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();
}
}
I know I'm a bit late at answering, but I had the same problem and I fixed it by simply changing
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, items);
to
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice, items);
I hope it helps someone.
As Quintin already mentioned in the comment, the problems reason maybe that the text color and background of the list items are the same. Use another view template for your list items, eg. 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;
}
}, ...
The layout inflater can be grabbed over a context:
final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
精彩评论