Android Listview自定义布局多选
我想创建一个允许多选的列表视图。典型的解决方案是获取光标并使用SimpleCursorAdapterAndroid Listview自定义布局多选,android,xml,listview,simplecursoradapter,Android,Xml,Listview,Simplecursoradapter,我想创建一个允许多选的列表视图。典型的解决方案是获取光标并使用SimpleCursorAdapter SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, R.layout.simple_list_item_multiple_choice, cur2, cols2, views2); 当我使用R.layout.simple\u list\u item\u multiple
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);
当我使用R.layout.simple\u list\u item\u multiple\u choose
时,我可以使它正常工作。当选择多个项目时,我会使用复选标记
所以我决定尝试一下定制的布局。下面是我的布局的XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="@+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>
</LinearLayout>
l2是我的listview。我没有查看CheckedTextView。。。根据我的理解,它应该在您单击该行时切换检查 我想你可以这样强迫它:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1);
chkTxt.toggle();
}
请注意,这将是一个黑客,你应该真正找到根本问题,但它可能会让你在这段时间
编辑
在谷歌搜索了很多之后,我发现了这个问题。。。行布局的根需要可检查才能使用CheckedTextView,而LinearLayout则不是
更多的谷歌搜索找到了解决方案。。。重写LinearLayout类
说明/代码我没有查看CheckedTextView。。。根据我的理解,它应该在您单击该行时切换检查 我想你可以这样强迫它:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1);
chkTxt.toggle();
}
请注意,这将是一个黑客,你应该真正找到根本问题,但它可能会让你在这段时间
编辑
在谷歌搜索了很多之后,我发现了这个问题。。。行布局的根需要可检查才能使用CheckedTextView,而LinearLayout则不是
更多的谷歌搜索找到了解决方案。。。重写LinearLayout类
说明/代码我遇到的问题是,复选标记没有显示为选中状态,这是因为布局错误。它应该没有线性布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="@+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
然后SimpleCursorAdapter可以正确设置“checkedTextView1”(具有可见效果)
在我的例子中,我使用了ArrayAdapter,它工作了
list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
R.id.category_checkedText, this.categories));
list.setAdapter(新阵列适配器)(此,R.layout.category_布局,
R.id.category_checkedText,this.categories));
“我遇到的问题是,复选标记没有显示为选中”的问题是因为您的布局错误。它应该没有线性布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="@+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
然后SimpleCursorAdapter可以正确设置“checkedTextView1”(具有可见效果)
在我的例子中,我使用了ArrayAdapter,它工作了
list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
R.id.category_checkedText, this.categories));
list.setAdapter(新阵列适配器)(此,R.layout.category_布局,
R.id.category_checkedText,this.categories));
您希望它在单击列表项时发生,还是在单击列表项中的复选框时发生?我希望它在单击列表项时发生。如果你知道如何做到两者兼顾,我不介意知道如何做到两者兼顾。THX使用CheckedTextView时,您不能同时执行这两项操作,我正在考虑TextView和Checkbox。不管怎样,你已经叫了两次LinearLayout。。。你的档案真的是这样吗?如果是,请删除第一个LinearLayout标注,看看会发生什么。不,不是。我将更新帖子。很抱歉造成混淆。您是如何将选择模式设置为“多个”的?(我一直在玩,到目前为止,没有被设置是唯一让它对我不起作用的事情)。你希望它在单击列表项时发生,还是在单击列表项中的复选框时发生?我希望它在单击列表项时发生。如果你知道如何做到两者兼顾,我不介意知道如何做到两者兼顾。THX使用CheckedTextView时,您不能同时执行这两项操作,我正在考虑TextView和Checkbox。不管怎样,你已经叫了两次LinearLayout。。。你的档案真的是这样吗?如果是,请删除第一个LinearLayout标注,看看会发生什么。不,不是。我将更新帖子。很抱歉造成混淆。您是如何将选择模式设置为“多个”的?(我一直在玩,到目前为止,没有被设置是唯一的事情,使它不适合我)。