如何将复选框添加到使用CursorAdapter的列表视图中

时间:2015-08-21 03:50:33

标签: android listview android-listview android-cursoradapter android-checkbox

我有一个listview,它使用CursorAdapter来填充列表视图。我想允许用户选择多个项目并同时对所有选定项目执行删除项目等操作。我尝试将CheckBox项添加到项item_vocab.xml的布局xml中。复选框显示在每个项目旁边,但我无法弄清楚如何跟踪已检查项目的状态。我还尝试添加setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)而不是复选框没有显示出来。

以下是我的一些代码:

item_vocab.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:longClickable="true">

        <ImageView
            android:layout_width="36sp"
            android:layout_height="36sp"
            android:id="@+id/vocabLevel"
            android:layout_gravity="right"
            android:src="@drawable/level_bars"
            android:scaleType="fitXY"
            android:contentDescription="@string/vocab_level"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/vocabName"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/vocabLevel"
            android:layout_toStartOf="@+id/vocabLevel"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/vocabDefinition"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/vocabLevel"
            android:layout_toStartOf="@+id/vocabLevel"
            android:layout_below="@id/vocabName"/>


    </RelativeLayout>

activity_my_vocab.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context="charlesli.com.personalvocabbuilder.io.MyVocabActivity">

        <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mVocabList"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/empty_text_view"
            android:id="@android:id/empty"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>

    </RelativeLayout>

MyVocabActivity.java

    public class MyVocabActivity extends ActionBarActivity {

        private VocabCursorAdapter mVocabAdapter;
        private ListView mVocabListView;
        private TextView mEmptyTextView;
        private Cursor mCursor;
        private String mSelectedVocab;
        private CheckBox mCheckBox;

        private VocabDbHelper mDbHelper = new VocabDbHelper(this);

        private ArrayList<String> mCheckedItems = new ArrayList<String>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_vocab);

            mVocabListView = (ListView) findViewById(R.id.mVocabList);
            mEmptyTextView = (TextView) findViewById(android.R.id.empty);
            mVocabListView.setEmptyView(mEmptyTextView);
            mCursor = mDbHelper.getCursorMyVocab(mDbHelper);
            mVocabAdapter = new VocabCursorAdapter(this, mCursor, 0);
            mVocabListView.setAdapter(mVocabAdapter);
            mVocabListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            mVocabListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    mSelectedVocab = (String) ((TextView) view.findViewById(R.id.vocabName)).getText();
                    editVocabAlertDialog(mSelectedVocab, view, position, id);
                    return true;
                }
            });


        }

VocabCursorAdapter.java

public class VocabCursorAdapter extends CursorAdapter {

    private static final int DIFFICULT = 0;
    private static final int FAMILIAR = 1;
    private static final int EASY = 2;
    private static final int PERFECT = 3;


    public VocabCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, 0);
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.item_vocab, parent, false);
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Find fields to populate in inflated template
        TextView tvVocabName = (TextView) view.findViewById(R.id.vocabName);
        TextView tvVocabDefinition = (TextView) view.findViewById(R.id.vocabDefinition);
        ImageView tvVocabLevel = (ImageView) view.findViewById(R.id.vocabLevel);

        String vocab = cursor.getString(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_VOCAB));
        String definition = cursor.getString(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_DEFINITION));
        int level = cursor.getInt(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_LEVEL));
        tvVocabName.setText(vocab);
        tvVocabDefinition.setText(definition);
        if (level == DIFFICULT) {
            tvVocabLevel.setImageResource(R.drawable.level_bars_difficult);
        }
        else if (level == FAMILIAR) {
            tvVocabLevel.setImageResource(R.drawable.level_bars_familiar);
        }
        else if (level == EASY) {
            tvVocabLevel.setImageResource(R.drawable.level_bars_easy);
        }
        else if (level == PERFECT) {
            tvVocabLevel.setImageResource(R.drawable.level_bars_perfect);
        }
    }
}

如果我需要提供更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

您可以使用CustomListview,创建另一个具有textview和checkbox的xml,并在自定义适配器类中使用该布局来填充列表,然后它将按您的需要工作。例如,查看本教程:

http://techlovejump.com/android-listview-with-checkbox/