我正在Eclipse中开发Android应用程序。目前我的目标是API级别3,但我错误地在Android 1.6模拟器(API级别4)上进行了测试。在1.6它工作正常,但在1.5我的ListView与CHOICE_MODE_SINGLE点击时不选择项目。
这是我的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中的项目在单击时不会检查。
有什么想法吗?
答案 0 :(得分:1)
看来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
还有其他人看到过这种行为吗?