使用SimpleAdapter和单选模式的AlertDialog不会显示单击的项目

时间:2013-03-21 14:47:22

标签: java android android-alertdialog android-adapter

我需要显示一个带有一些自定义行的AlertDialog。这些行应该在右侧有一个单选按钮,选择模式应该是单模式(因此单选按钮)。

我有什么:

ArrayList<HashMap<String, String>> it = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map;

map = new HashMap<String, String>();
map.put("asd", "test1");
it.add(map);
map = new HashMap<String, String>();
map.put("asd", "test2");
it.add(map);
map = new HashMap<String, String>();
map.put("asd", "test3");
it.add(map);

SimpleAdapter sa = new SimpleAdapter(context, it, R.layout.dialog_row, new String[] { "asd" }, new int[] { android.R.id.text1 });

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setSingleChoiceItems(sa, -1, null);
// here are added positive and negative buttons
builder.show();

dialog_row的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/rowIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/rh_3" />

<CheckedTextView
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?listPreferredItemHeightSmall"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:gravity="center_vertical"
    android:paddingLeft="?listPreferredItemPaddingLeft"
    android:paddingRight="?listPreferredItemPaddingRight"
    android:textAppearance="?textAppearanceListItemSmall" />

</LinearLayout>

AlertDialog按照我的预期显示 - 但是当我点击某个项目时,单选按钮仍未选中。这是为什么?

1 个答案:

答案 0 :(得分:1)

我在这里找到了一个有效的解决方案:https://stackoverflow.com/a/3281239/554002

基本上我必须自定义ArrayAdapter并使用CheckedTextView作为布局xml中的唯一元素,而不是上面发布的元素。

相关问题