listView中的搜索功能

时间:2012-12-07 13:32:32

标签: android listview search

我想在listView中搜索产品名称,但在我键入搜索文本字段内的某些文本后,列表中没有任何内容显示。我没有使用ArrayAdapter,但我正在使用ResourceCursorAdapter。

   public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                helper = new ComicsData(this);
                helper.open();
                listSearch=(EditText)findViewById(R.id.inputSearch);
                listSearch.addTextChangedListener(filterTextWatcher);
                model = helper.getAllProduct(list);
                startManagingCursor(model);
                adapter=new ShoppingListAdapter(this, model);

                listView.setAdapter(adapter);
    }
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) {
                //String search=listSearch.getText().toString();
                //int textLength=search.length();
                adapter.getFilter().filter(s);

            }

        };

ShoppingListAdapter

class ShoppingListAdapter extends ResourceCursorAdapter {

        private final String CN = null;

        public ShoppingListAdapter(Context context, Cursor c) {
            super(context, R.layout.productrow, c);
            // TODO Auto-generated constructor stub
        }
    @Override
        public void bindView(View row, Context context, Cursor c) {
            // TODO Auto-generated method stub


            listName = (TextView) row.findViewById(R.id.produtName);
    listName.setText(helper.getProductName(c));

有人知道我的错吗?

2 个答案:

答案 0 :(得分:1)

更改listSearch中的文字后,根据搜索文本helper.getProducts(searchText);获取项目并致电adapter.notifyDataSetChanged()

答案 1 :(得分:0)

您需要为适配器提供FilterQueryProvider的实例。此提供程序接受过滤器查询并返回带有所有找到项的游标。