多选择可搜索ListView

bur*_*mat 11 android android-searchmanager multiple-choice android-listview

好的我有一个多选ListView工作正常.我检查联系人的框(保存在String []中)并可以返回值.因为有些人有一堆联系我想创建一个搜索栏,有点像Android手机书的库存.我创建了一个EditText并将其对齐在我的列表上方.我在StackOverflow上找到了过滤代码,它运行得非常好.

我的问题:

当您过滤某些人的名字,并选择名称时,当您从EditText退格或继续键入时,不会保存您选择的名称的正确位置.例如,如果我开始键入"Adam"并进入"Ada"并选择它,如果我退格键入"Carol",则选择"Ada"所处的位置.它收集了"亚当"在点击时所处的位置(比方说2),当列表恢复时,检查位置(2),即使亚当不在那里.我需要一种方法来收集名称..然后当再次恢复或搜索列表时,将检查NAME Adam,而不是之前的位置亚当.除了创建大量数组之外,我绝对没有任何想法,并且可以真正使用一些帮助.下面是我正在使用的一些代码:

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


        myListView = (ListView)findViewById(android.R.id.list);
        search_EditText = (EditText) findViewById(R.id.search_EditText);
        search_EditText.addTextChangedListener(filterTextWatcher);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, ContactsList); 
        setListAdapter(adapter);

        myListView.setItemsCanFocus(false);
        getListView().setChoiceMode(2);
        myListView.setTextFilterEnabled(true);
        myListView.setFastScrollEnabled(true);
        myListView.invalidate();
}

    private TextWatcher filterTextWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            adapter.getFilter().filter(s);

        }

    };
Run Code Online (Sandbox Code Playgroud)

bur*_*mat 10

我从未找到比我下面列出的方法更好的方法.我不再使用这个代码,如果有更好的解决方案,我希望有人将它发布到某个地方.

我最终做的是创建一个ArrayList来保存所选名称.如果选择了名称,则会将名称推入ArrayList,如果未选中,则会从列表中弹出.afterTextChanged选择何时,将迭代列表,并检查名称是否当前在适配器中列出.当您完成选择过程并想要继续时,我清除EditText以清除过滤器,因此填充ListView中的完整列表并将所有联系人设置为选中(如果它们存在于ArrayList中).

注意,我使用自定义适配器作为仅列出名称的联系人列表,因此如果您使用其他类型的数据,此解决方案可能会更加混乱,我将此方法视为黑客解决方案:

/** Used for filter **/
private TextWatcher filterTextWatcher = new TextWatcher() {

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        ListView listview = getListView();

        SparseBooleanArray checked = listview.getCheckedItemPositions();
        for (int i = 0; i < ContactsList.length; i++) {
            if (checked.get(i) == true) {
                Object o = getListAdapter().getItem(i);
                String name = o.toString();
                // if the arraylist does not contain the name, add it
                if (selected.contains(name)){ 
                    // Do Nothing
                } else {
                    selected.add(name);
                }
            }
        }           
    } //<-- End of beforeTextChanged

    public void onTextChanged(CharSequence s, int start, int before, int count) {           
        adapter.getFilter().filter(s);              
    } //<-- End of onTextChanged

    public void afterTextChanged(Editable s) {
        ListView listview = getListView();
        // Uncheck everything:
        for (int i = 0; i < listview.getCount(); i++){
            listview.setItemChecked(i, false);                  
        }

        adapter.getFilter().filter(s, new Filter.FilterListener() {
            public void onFilterComplete(int count) {
                adapter.notifyDataSetChanged();
                ListView listview = getListView();
                for (int i = 0; i < adapter.getCount(); i ++) {
                    // if the current (filtered) 
                    // listview you are viewing has the name included in the list,
                    // check the box
                    Object o = getListAdapter().getItem(i);
                    String name = o.toString();
                    if (selected.contains(name)) {
                        listview.setItemChecked(i, true);
                    } else {
                        listview.setItemChecked(i, false);
                    }
                }

            }
        });         
    } //<-- End of afterTextChanged

}; //<-- End of TextWatcher
Run Code Online (Sandbox Code Playgroud)

您不希望在使用过滤器时使用索引,因为索引1可能在一个中ListView,而ListView在更改过滤器时它可能是另一个项目.