我正在 Eclipse 中开发适用于 Android 的应用程序。目前我的目标是 API 级别 3,但我错误地在 Android 1.6 模拟器(API 级别 4)上进行了测试。在 1.6 上它工作正常,但在 1.5 上我的带有 CHOICE_MODE_SINGLE 的 ListView 在单击时不选择项目。
这是我的 ListView XML:
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="@+id/ListDomains"
android:layout_margin="5px"
android:choiceMode="singleChoice"
android:clickable="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
>
</ListView>
这是 ListView 中项目的 XML:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:id="@+id/domain_list_value"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
>
</CheckedTextView>
我创建了一个自定义 ArrayList 适配器来让我自定义 getView。这是 DomainArrayAdapter 的代码:
public class DomainArrayAdapter extends ArrayAdapter<char[]> {
private LayoutInflater mInflater;
public DomainArrayAdapter(Context context, int textViewResourceId,
List<char[]> objects) {
super(context, textViewResourceId, objects);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.domain_list, null);
}
char[] text = super.getItem(position);
((CheckedTextView)convertView).setText(text, 0, text.length);
return convertView;
}
}
所有这些代码都可以很好地针对 API 级别 3 编译并在 Android 1.6 模拟器上运行。但是,针对 1.5 模拟器运行时,ListView 中的项目在单击时不会检查。
有什么想法吗?
最佳答案
Android 1.5 似乎不遵守 ListView XML 中设置的 choiceMode。以编程方式设置它会使其正常工作:
ListView listDomains = (ListView) findViewById(R.id.ListDomains);
Log.d("app", String.valueOf(listDomains.getChoiceMode())); //prints 0
listDomains.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Log.d("app", String.valueOf(listDomains.getChoiceMode())); //prints 1
还有其他人见过这种行为吗?
关于android - ListView 不检查 Android 1.5 上的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215518/