Android cursoradapter过滤列表视图

时间:2014-02-06 03:25:58

标签: android listview

我正在尝试使用cursoradapter过滤我的listview。这是我到目前为止所尝试的:

CustomAdapter adapter;
Cursor cursor;

etSearch.addTextChangedListener(new TextWatcher (){

        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

            adapter.getFilter().filter(cs.toString());

        }

    });

cursor = dbHelper.getAllItemInventoryListings();

    adapter = new CustomAdapter(this, cursor);

    lv.setAdapter(adapter);
    lv.setTextFilterEnabled(true);

    adapter.setFilterQueryProvider(new FilterQueryProvider() {

        @Override
        public Cursor runQuery(CharSequence constraint) {

            String strItemCode = constraint.toString();
            return dbHelper.getAllItemInventoryListings(strItemCode);

        }
    });

当我尝试它时不会过滤。什么似乎是错的?我们将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:5)

尝试在adapter.notifyDataSetChanged();

之后添加adapter.getFilter().filter(cs.toString());