Android 如何在列表视图中添加复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2538539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add a checkbox in a listview?
提问by Bugzy bug
i have a question, been stuck for a while, i dont know how can i add a checkbox in the list, for example if I have a list of items i want to be able to check them. my xml code is the following:
我有一个问题,被卡住了一段时间,我不知道如何在列表中添加一个复选框,例如,如果我有一个我希望能够检查它们的项目列表。我的 xml 代码如下:
<LinearLayout android:id="@+id/topLayout"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_centerHorizontal="true" android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:id="@+id/middleLayout"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout android:id="@+id/leftMiddleLayout"
android:orientation="vertical" android:layout_below="@+id/topLayout"
android:layout_above="@+id/bottomLayout"
android:layout_width="60px" android:layout_height="wrap_content"
>
<ListView android:id="@+id/checkboxList" android:layout_width="fill_parent"
android:layout_height="wrap_content" ></ListView>
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:checked="false"
android:text="test">
</CheckBox>
</LinearLayout>
<LinearLayout android:id="@+id/rightMiddleLayout"
android:orientation="vertical" android:layout_below="@+id/topLayout"
android:layout_above="@+id/bottomLayout"
android:layout_width="280px" android:layout_height="wrap_content"
>
<ListView android:id="@+id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" ></ListView>
<TextView android:id="@+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="@+id/bottomLayout"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingBottom="5pt"
>
<EditText android:id="@+id/insertNewItem"
android:layout_width="220px" android:layout_height="wrap_content" />
<TextView android:layout_width="10px" android:layout_height="wrap_content" />
<Button android:id="@+id/addItemButton" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:text="Add Item"/>
</LinearLayout>
if you have any ideas please let me know, its for my academic studies :((
如果您有任何想法,请告诉我,这是为了我的学术研究:((
Thank you!
谢谢!
采纳答案by Dave.B
public class myAdapter extends SimpleCursorAdapter {
public myAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
super(context, layout, cursor, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
cb=(CheckBox)view.findViewById(R.id.cb);
cb.setText(dbgetStringValue);
cbText=(TextView)view.findViewById(R.id.cbText);
cbText.setText(dbgetStringValue);
cb.setChecked(false);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if(cb.isChecked()) {
// action
}
else if(isChecked==false) {
// action
}
}
});
}
}
is that what you want?
那是你要的吗?
回答by Kalu Khan Luhar
final ListView lView = (ListView) findViewById(R.id.ListView01);
lView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Here item is an array.
这里 item 是一个数组。
回答by Dave.B
I recommend you watch this video from the android dev site on how to Make your Android UI Fast and Efficient. It will give you code to solve your problem and show you the proper way to implement your adapter to make sure it's as fast as can be.
我建议您从 android 开发站点观看此视频,了解如何使您的 Android UI 快速高效。它将为您提供解决问题的代码,并向您展示实现适配器的正确方法,以确保它尽可能快。
回答by Jim Blackler
If you use a ListAdapter (e.g. a customized BaseAdapter) on your listView with setAdapter() you can use LayoutInflater inside your adapter's getView() to supply any layout you like for your list entries. This can include a CheckBox.
如果您使用 setAdapter() 在您的 listView 上使用 ListAdapter(例如自定义 BaseAdapter),您可以在您的适配器的 getView() 中使用 LayoutInflater 来为您的列表条目提供您喜欢的任何布局。这可以包括一个复选框。
回答by Jim Blackler
Or you can extends any ListAdapter to a subclass and override bindView. Inside of if you would set .setText for the ChecBoxes!
或者您可以将任何 ListAdapter 扩展到子类并覆盖 bindView。在里面 if 你会为 CheckBoxes 设置 .setText !