简体   繁体   English

如何在 android 可搜索列表视图中保留选中的复选框?

[英]how to keep selected checkboxes in android searchable listview?

I have a simple searchable listview with checkboxes but it can't remember my selections when it's getting filtered.我有一个带有复选框的简单可搜索列表视图,但是当它被过滤时它不记得我的选择。 when I filter a value and mark related checkbox then my choice won't be kept after clearing search value...当我过滤一个值并标记相关复选框时,清除搜索值后我的选择将不会保留...

activity_main.xml activity_main.xml

<?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/btnChecked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:foregroundGravity="center_horizontal"
        android:text="Get checked items"></Button>

    <EditText
        android:id="@+id/searchFilter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Search Ingredients"
        android:textColor="@color/material_on_surface_emphasis_medium" />

    <ListView
        android:id="@+id/lvMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

MainActivity.java MainActivity.java

package com.example.listview;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {

    final String LOG_TAG = "myLogs";

    ListView lvMain;
    String[] names;

    private ArrayAdapter<CharSequence> adapter;



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

        EditText theFilter = (EditText) findViewById(R.id.searchFilter);

        lvMain = (ListView) findViewById(R.id.lvMain);
        // here we adjust list elements choice mode
        lvMain.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        // create adapter using array from resources file
        adapter = ArrayAdapter.createFromResource(
                this, R.array.names,
                android.R.layout.simple_list_item_multiple_choice);
        lvMain.setAdapter(adapter);

        Button btnChecked = (Button) findViewById(R.id.btnChecked);
        btnChecked.setOnClickListener(this);

        // get array from resources file
        names = getResources().getStringArray(R.array.names);

        theFilter.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


                (MainActivity.this).adapter.getFilter().filter(charSequence);
                SparseBooleanArray scArray = lvMain.getCheckedItemPositions();




            }

            @Override
            public void afterTextChanged(Editable editable) {


                // Uncheck everything:

                for (int i = 0; i < lvMain.getCount(); i++) {
                    if (lvMain.isItemChecked(i)) {
                        lvMain.setItemChecked(i, true);
                    } else {
                        lvMain.setItemChecked(i, false);
                    }
                }
            }

        });
    }

    public void onClick(View arg0) {
        Log.d(LOG_TAG, "checked: ");
        SparseBooleanArray sbArray = lvMain.getCheckedItemPositions();
        for (int i = 0; i < sbArray.size(); i++) {
            int key = sbArray.keyAt(i);
            if (sbArray.get(key))
                Log.d(LOG_TAG, names[key]);
        }
    }
}

as you can see in below pictures, it can't remember "Petr" and "Ivan" selected wrongly after search...如下图所示,它不记得搜索后错误选择了“Petr”和“Ivan”...

搜索前

选择过滤值

清除搜索后选择错误

It is because ArrayAdapter does not support this.这是因为ArrayAdapter不支持这个。 You need to create a custom adapter that will remember what has been checked so that when it redraws a view it does not take the default value of unchecked.您需要创建一个自定义适配器,该适配器将记住已检查的内容,以便在重绘视图时不会采用未选中的默认值。

Create a class that extends from BaseAdapter that works on an ArrayList<Model> instead of string array.创建一个从 BaseAdapter 扩展的BaseAdapter ,它适用于ArrayList<Model>而不是字符串数组。 In this Model you can have a boolean checked (initially all false).在此 Model 中,您可以检查 boolean(最初全部为假)。 That way when user scrolls around or searches your model will know weather current name is selected.这样,当用户滚动或搜索您的 model 时,就会知道当前天气名称已被选中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM