简体   繁体   English

SimpleCursorAdapter / ArrayAdapter填充数据库并过滤listview

[英]SimpleCursorAdapter/ArrayAdapter populating database and filtering listview

I just got lost. 我只是迷路了。 I'm populating database using SimpleCursorAdapter to listview . 我正在使用SimpleCursorAdapter填充数据库到listview I'd like to filter it with EditText , but every question/code/example/tutorial is simply not working for me. 我想用EditText过滤它,但是每个问题/代码/示例/教程对我都不起作用。 But I've seen many using ArrayAdapter .The question is that, can I populate listview with a SimpleCursorAdapter and them filter it using EditText with an ArrayAdapter ? 但是我已经看到很多使用ArrayAdapter问题了。问题是,我可以用SimpleCursorAdapter填充listview并使用带有ArrayAdapter EditText对其进行过滤吗?

You must use filter method for filter with edittext.Like above, 您必须将filter方法用于带有edittext的过滤器。像上面的一样,

    kisiText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if(s.toString().isEmpty()){
                kisiInfoList.clear();
                listeyiCek();
                adapter = new RehberListAdapter(RehberActivity.this, kisiInfoList);
                rehberListView.setAdapter(adapter);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            adapter.getFilter().filter(s.toString());

        }
    });

And here is a filter method that your adapter includes, 这是您的适配器包括的一种过滤方法,

@Override
public Filter getFilter() {
    return new Filter() {
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {        

            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                kisiList = (List<Kisi>) results.values;
                notifyDataSetChanged();
            }
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                results.values = kisiList;
                results.count = kisiList.size();
            } else {
                List<Kisi> nPlanetList = new ArrayList<Kisi>();

                for (Kisi p : kisiList) {
                    if (p.getAdSoyad()
                            .toUpperCase()
                            .startsWith(constraint.toString().toUpperCase()))
                        nPlanetList.add(p);
                }

                results.values = nPlanetList;
                results.count = nPlanetList.size();

            }
            return results;
        }
    };
}

When you enter text it is filtering and restricting your listView 当您输入文字时,它会过滤并限制您的listView

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

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