Android 如何在列表活动中激活复选标记?

Android 如何在列表活动中激活复选标记?,android,listactivity,android-arrayadapter,checkmark,Android,Listactivity,Android Arrayadapter,Checkmark,我有一个ListActivity,其数组适配器声明为arrayAdapter=newArrayAdapter(这是android.R.layout.simple\u list\u item\u选中的)这显示了一组在最右侧带有复选标记的行。您能告诉我如何获取对这些复选标记的引用,或者如何选中/取消选中它们吗?我能做的最接近的事情是使用以下命令在单击单元格后更改复选标记: @Override protected void onListItemClick( ListView l, View v, in

我有一个ListActivity,其数组适配器声明为
arrayAdapter=newArrayAdapter(这是android.R.layout.simple\u list\u item\u选中的)这显示了一组在最右侧带有复选标记的行。您能告诉我如何获取对这些复选标记的引用,或者如何选中/取消选中它们吗?

我能做的最接近的事情是使用以下命令在单击单元格后更改复选标记:

@Override
protected void onListItemClick( ListView l, View v, int position, long id)
{
  CheckedTextView textView = (CheckedTextView)l.getChildAt(position);
  text.setChecked(!textView.isChecked());

  super.onListItemClick (l, v, position, id);
}

我仍然希望能够在用户不接触任何单元格的情况下设置复选标记。

CheckedTextView本身处理复选框。它作为onListItemClick处理程序中的第二个参数(视图v)传入。因此,您可以将代码简化如下:

@Override
protected void onListItemClick( ListView l, View v, int position, long id)
{
  CheckedTextView textView = (CheckedTextView)v;
  textView.setChecked(!textView.isChecked());
}

我有一个类似的问题,并尝试了这里提供的解决方案,但我仍然有很多问题。 我只想有一个列表,其中包含可选择的“测试用例”和两个按钮“选择所有测试”和“运行所选测试”(因此我不想只有一个ListActivity)。 如“JDC”getChildAt(和getChildCount)所述,指的是当前显示的项目,但我的列表不适合屏幕,因此我无法使用它来选择所有列表项目。 此外,如果我使用了CheckedTextViewsetChecked,我会遇到一个问题,即当我滚动列表时,选择就消失了。 我的解决方案是下面的代码,它通过使用列表视图的getCountsetItemChecked修复了这些问题(另请参见源代码中的注释)。此外,它还显示了如何检索选中的项

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;

public class TestActivity extends Activity {

    static final String[] names = new String[] { "Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6", "Test 7", "Test 8", "Test 9", "Test 10"};
    ListView list;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create an ArrayAdapter, that will actually make the Strings above
        // appear in the ListView
        list = (ListView)findViewById(R.id.listOfTests);
        list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, names));
    }

// This does not work.
// 1st: it checks only the displayed items
// 2nd: as soon as you scroll the list the selections are undone
//
//    public void onRunAllTestsClick (View view) {
//      int count = list.getChildCount();
//      for (int i = 0; i < count; i++)
//          ((CheckedTextView)list.getChildAt(i)).setChecked(true);
//    }

    // This is the solution
    // 1st: getCount deliveres the count of all list items (even if they are not displayed)
    // 2nd: calling setItemChecked on the ListView ensures that the ListView "knows" that the item is checked and does not destroy it if you scroll the list
    public void onRunAllTestsClick (View view) {
        int count = list.getCount();
        for (int i = 0; i < count; i++)
            list.setItemChecked(i, true);
    }

    public void onRunSelectedTestsClick (View view) {
        SparseBooleanArray resultArray = list.getCheckedItemPositions();
        int size = resultArray.size();
        for (int i = 0; i < size; i++)
            if (resultArray.valueAt(i))
                Log.i("CodecTestActivity", list.getAdapter().getItem(resultArray.keyAt(i)).toString());
    }
}
package com.example.test;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.util.SparseBooleanArray;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.CheckedTextView;
导入android.widget.ListView;
公共类测试活动扩展了活动{
静态最终字符串[]名称=新字符串[]{“测试1”、“测试2”、“测试3”、“测试4”、“测试5”、“测试6”、“测试7”、“测试8”、“测试9”、“测试10”};
列表视图列表;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个ArrayAdapter,它将实际生成上面的字符串
//显示在列表视图中
列表=(ListView)findViewById(R.id.listOfTests);
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_多项选择,名称));
}
//这是行不通的。
//第一:它只检查显示的项目
//第二:滚动列表后,选择即被撤消
//
//RunAllTestsClick(视图)上的公共无效{
//int count=list.getChildCount();
//for(int i=0;i
以下是相应的布局(main.xml):


请注意,使用
l.setItemChecked(位置,!l.isItemChecked(位置))如果您的列表视图扩展为可检查的
!(android.R.layout.simple_list_item_checked)如果只检查
CheckedTextView
,它不会更新基础数据,例如isItemChecked()不会更改。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linearLayout1">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onRunSelectedTestsClick" android:id="@+id/RunSelectedTestsClick" android:text="@string/runselectedtests"></Button>
        <Button android:text="@string/runalltests" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onRunAllTestsClick" android:id="@+id/RunAllTests"></Button>
    </LinearLayout>
    <ListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/listOfTests" android:choiceMode="multipleChoice"></ListView>
</LinearLayout>