【问题标题】:Search function in listViewlistView中的搜索功能
【发布时间】:2012-12-07 13:32:32
【问题描述】:

我想在 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);

            }

        };

购物清单适配器

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));

有人知道我的错误吗?

【问题讨论】:

    标签: android listview search


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我是否需要在查询部分使用 LIKE 语句?
      猜你喜欢
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多