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

时间:2015-03-24 13:53:45

标签: android database listview

我迷路了。我使用SimpleCursorAdapter填充数据库到listview。我想用EditText过滤它,但每个问题/代码/示例/教程都不适合我。但我发现许多人使用ArrayAdapter。问题是,我是否可以使用SimpleCursorAdapter填充listview并使用EditText使用ArrayAdapter过滤它?< / p>

1 个答案:

答案 0 :(得分:-1)

您必须使用带有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());

        }
    });

这是适配器包含的过滤方法,

@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;
        }
    };
}

当您输入文字时,它会过滤并限制您的listView