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

发布于 2024-08-20 04:26:19 字数 201 浏览 6 评论 0原文

我有一个 ListActivity ,其数组适配器声明为 arrayAdapter = new ArrayAdapter; (this, android.R.layout.simple_list_item_checked); 这显示了一堆在最右侧带有复选标记的行。您能告诉我如何获取这些复选标记的参考或如何选中/取消选中它们吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐偏执 2024-08-27 04:26:19

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

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

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());
}
音栖息无 2024-08-27 04:26:19

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

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

这也是适当的布局(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>

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>
冷月断魂刀 2024-08-27 04:26:19

我能做的最接近的事情是在单击单元格后更改复选标记:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文