可勾选列表视图光标数据的独立复选框

可勾选列表视图光标数据的独立复选框

本文介绍了可勾选列表视图光标数据的独立复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是创建的的ListView根据 ListActivity 。数据内容来自数据库 SimpleCursorAdapter

My task is to create a ListView based on ListActivity. The data content comes from a database SimpleCursorAdapter.

从光标列的独立IT在每个列表项显示一个复选框。用户应检查尽可能多的行,因为他想要的。当用户完成了他的选择,我必须得到所有选中的行,它的_id列,所以我必须知道被检查的行。

Independent from cursor columns it have to show a CheckBox in each listitem. The user should check as many rows as he want. When the user finished his selection, I have to get all of checked rows and it's _id columns, so I have to know which rows were checked.

怎么办呢?

没有的CheckBox 列表视图功能已正常工作。

Without CheckBoxes the Listview functionality already works fine.

推荐答案

一种快捷方法是使用 android.R.layout.simple_list_item_multiple_choice 。这code例如你想要做什么(只要注意,我用我的光标联系人):

One quick way would be to use android.R.layout.simple_list_item_multiple_choice. This code example does what you want (just note that I used Contacts for my Cursor):

public class ListCheck extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_check);

        this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        startManagingCursor(cursor);

        String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME };
        int[] to = new int[] { android.R.id.text1 };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_multiple_choice, cursor, columns, to);
        this.setListAdapter(adapter);

        Button finishButton = (Button) this.findViewById(R.id.finishButton);
        finishButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                SimpleCursorAdapter adapter = (SimpleCursorAdapter) ListCheck.this.getListAdapter();
                Cursor cursor = adapter.getCursor();

                ListView lv = ListCheck.this.getListView();
                SparseBooleanArray selectedItems = lv.getCheckedItemPositions();
                for (int i = 0; i < selectedItems.size(); i++) {

                    int selectedPosition = selectedItems.keyAt(i);
                    cursor.moveToPosition(selectedPosition);
                    Log.d("", cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts.DISPLAY_NAME))+" is checked");
                    Log.d("", "row id: "+adapter.getItemId(selectedPosition));
                }
            }
        });
    }
}

活动的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/finishButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice" />

</LinearLayout>


不过,如果你想添加的每一行(图像,字幕..)内的其他自定义数据,那么你必须创建使用的。 CheckedTextView 是真正的关键在这里,因为它使用的 android.R.layout.simple_list_item_multiple_choice


However, if you want to add other custom data inside each row (image, subtitle..), then you have to create a custom layout for your rows using CheckedTextView. CheckedTextView is really the key here, since it's used by the android.R.layout.simple_list_item_multiple_choice.

只要按照这个例子:

<com.mfp.tests.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <CheckedTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="added a data: subtitle"/>

</com.mfp.tests.CheckableLinearLayout>

最后一件事:我使用了一个自定义布局,而不是的LinearLayout
为了使 CheckedTextView 响应(自动检查,或当你点击一排没有),布局必须实施可勾选(与LinearLayout中没有),所以你必须在 CheckableLinearLayout 类从这个复制的:

One last thing: I used a custom layout instead of a LinearLayout.In order to make the checkBox of CheckedTextView responsive (automatically checked or unchecked when you click on a row), the layout must implement Checkable (and LinearLayout doesn't), so you'll have to copy the CheckableLinearLayout class from this link:

PS:如果你想尝试上述code,不要忘了把&LT;使用许可权的android:NAME =android.permission.READ_CONTACTS/&GT; 您的Manifest.xml在

PS: If you want to try the code above, don't forget to put <uses-permission android:name="android.permission.READ_CONTACTS" /> inside your Manifest.xml

这篇关于可勾选列表视图光标数据的独立复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:59