列表视图 - 多项选择&可筛选

时间:2015-11-20 05:32:06

标签: android listview search filter

我正在尝试使用多个选择开发Listview以及在Listview中过滤文字。

下面是我的代码,

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        for (int i = 0; i < 20; i++) {
            items.add("abc " + i);
        }

        editText = (EditText) findViewById(R.id.editText);
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                //if (isOpened) {
                if (listadapter != null)
                    listadapter.getFilter().filter(s.toString().trim());

                //}
            }

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

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        listView = (ListView) findViewById(R.id.listView);
        listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        listadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, items);
        listView.setAdapter(listadapter);
        listView.setTextFilterEnabled(true);
    }

我选择了第1项,并且在搜索了一些文本之后,它显示了我选择的搜索项目。

有办法处理这个

检查图片

enter image description here

enter image description here

0 个答案:

没有答案