简体   繁体   English

(Pre)使用'simple_list_item_checked'选择ListActivity中的复选框

[英](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.

相关问题 Android listview simple_list_item_checked - Android listview simple_list_item_checked ArrayIndexOutOfBoundException simple_list_item_checked中的错误 - ArrayIndexOutOfBoundException Error in simple_list_item_checked 使用simple_list_item_checked,无论我单击哪里,都将对其进行检查 - Using simple_list_item_checked, wherever I click it's getting checked 在Cursor中的simple_list_item_checked中设置复选框 - Set checkbox in simple_list_item_checked from Cursor 在用户交互之前检查 simple_list_item_checked 的项目 - check items of simple_list_item_checked before user interaction 我正在使用android simple_list_item_checked单击该如何突出显示它 - I'm using android simple_list_item_checked how should I highlight it when I click it 自定义CursorAdapter绑定到simple_list_item_checked - Custom CursorAdapter to bind to simple_list_item_checked android listview 使用 simple_list_item_checked onItemLongClick 未按预期检查 - android listview using simple_list_item_checked onItemLongClick not checking as expected simple_list_item_multiple_choice和simple_list_item_checked有什么区别? - what is the difference between simple_list_item_multiple_choice and simple_list_item_checked? 如何使用simple_list_item_checked保存和恢复选中的项目? - How can I save and restore checked items with simple_list_item_checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM