【发布时间】:2012-12-20 08:57:40
【问题描述】:
我正在使用用于我的 ListView 的自定义适配器。创建 ArrayList 后
WifiListAdapter<WifiListItem> adapter = new WifiListAdapter<WifiListItem>(
this, R.layout.listview_item_text, listItems);
我的应用中有以下代码:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
但是,当我尝试单击复选框时,没有任何反应。所以我必须手动管理切换复选框状态。
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView checkBox = (CheckedTextView) v
.findViewById(R.id.text1);
if (checkBox != null) {
checkBox.toggle();
然后就可以了。 (在此之前我必须删除 setChoiceMode 方法调用)
那么可能是什么问题?为什么没有效果?
我的 R.layout.listview_item_text.xml
<TextView
android:id="@+id/topText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:paddingTop="7dp"
android:textColor="?android:attr/textColorPrimary"/>
<TextView
android:id="@+id/botText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:paddingTop="2dp"
android:paddingBottom="7dp"
android:textColor="?android:attr/textColorTertiary"
/>
在我的适配器中,我使用以下代码填充复选框布局:
convertView = mInflater.inflate(
R.layout.listview_item_checkbox, null);
CheckedTextView checkbox = ((CheckedTextView) convertView
.findViewById(R.id.text1))
listview_item_checkbox.xml
<CheckedTextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="22sp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
【问题讨论】:
-
我猜这是你的布局中的一些东西(R.layout.listview_item_text)。可以发一下吗?
-
这是因为您正在创建自己的适配器“WifiListAdapter”,而不是使用标准的 ArrayAdapter。
-
你能发布你的 text1.xml 吗?而且您不需要此代码 - if (checkBox != null) checkBox.toggle();
-
查看这个question的答案
-
@Mohamed_AbdAllah 我并没有真正理解。这是否意味着现在可以在自定义 Adapter 上调用此方法?
标签: android android-layout android-listview