[英](Pre)select checkboxes in a ListActivity using 'simple_list_item_checked'
I'm using the ListActivity class in conjunction with the simple_list_item_checked -layout which implements simple list items with checkboxes . 我正在将ListActivity类与simple_list_item_checked -layout结合使用,它实现了带复选框的简单列表项。 Everything is working fine - clicking added items calls onListItemClick() and I can check/uncheck respective checkboxes of entries via the 'View v' parameter.
一切正常 - 单击添加的项目调用onListItemClick(),我可以通过'View v'参数检查/取消选中相应的条目复选框。
However what I wasn't able to figure out yet is, how to (pre)select checkboxes without any userinteraction? 然而,我还没想到的是,如何(预)选择没有任何用户交互的复选框?
Minimal so far working code snippet showing my intend: 到目前为止最小的工作代码片段显示我的意图:
package org.test;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
public class TestActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked);
list_elems.add("foobar");
//TODO: check added entry labeled "foobar"
setListAdapter(list_elems);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
CheckedTextView check = (CheckedTextView)v;
check.setChecked(!check.isChecked());
}
}
Thanks a lot in advance! 非常感谢提前!
daten 回到名单Daten
This works for me: 这对我有用:
You have to set the choicemode of the underlying ListView to single or multiple. 您必须将基础ListView的choicemode设置为单个或多个。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, Android.R.layout.simple_list_item_checked);
list_elems.add("foobar");
//TODO: check added entry labeled "foobar"
setListAdapter(list_elems);
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemChecked(0, true);
}
Use a SimpleListAdapter
as the ListAdapter for your ListActivity. 使用
SimpleListAdapter
作为ListActivity的ListAdapter。 Use two columns (one for your string and the other for the checked value), and the system should take care of it by itself. 使用两列(一列用于字符串,另一列用于检查值),系统应自行处理。 Here is a good example
这是一个很好的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.