How do you activate checkmarks in list activity?

2020-07-24 05:18发布

问题:

I have a ListActivity with an array adapter declared like arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_checked); This shows a bunch of rows with checkmarks on the far right. Can you tell me how to get a reference to those checkmarks or how to check/uncheck them?

回答1:

The CheckedTextView itself handles the checkbox. It is passed in as the second argument (View v) in the onListItemClick handler. So, you can simplify your code as follows:

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


回答2:

I had a similar problem and tried the solutions provided here but I still had a lot of problems. I just wanted to have a list with selectable "test cases" and two buttons "select all tests" and "run selected tests" (therefore I didn't want to have just a ListActivity). As mentioned by "JDC" getChildAt (and getChildCount) refer to the currently displayed items but my list didn't fit on the screen and therefore I couldn't use it to select all list items. Additionally if I used setChecked of the CheckedTextView I had the problem that the selection disappeared as soon as I scrolled the list. My solution is the code below that fixes these issues by using getCount and setItemChecked of the ListView (see also the comments in the source code). Additionally it shows how to retrieve the checked items.

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());
    }
}

Here is also the appropriate layout (main.xml):

<?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>


回答3:

The closest I can do is change the checkmark after a cell is clicked using:

@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);
}

I would still like to be able to set the checkmarks without the user touching any cells.