我正在使用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>
这是列表视图中各项的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
还有其他人看到过这种行为吗?