有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

具有SimpleAdapter和单选模式的java AlertDialog不显示单击的项目

我需要显示一个带有一些自定义行的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[] { 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="horizontal" >

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

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

</LinearLayout>

AlertDialog如我所期望的那样显示-但是当我单击一个项目时,单选按钮保持未选中状态。为什么会这样


共 (1) 个答案

  1. # 1 楼答案

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

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