在listview上搜索(活动扩展ListActivity)

时间:2011-08-19 12:03:41

标签: android listview filter

我在我的活动中扩展ListActivity并绑定ListView。应用过滤器到listview。使用emulater时过滤器工作正常,因为我有笔记本电脑的键盘。但是当我想在android上过滤数据时问题就出现了,因为它没有向我显示键盘。代码如下。

String[] Customers = {"Alester","Lee","Broad","James"};
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, Customers));

  ListView lv = getListView();
  lv.setTextFilterEnabled(true); 

1 个答案:

答案 0 :(得分:3)

将EditText放在某个位置,例如在listView的顶部,然后添加TextWatcher,如:

    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) {
            _myAdapter.getFilter().filter(s);
        }
    };
    _filterText = (EditText) findViewById(R.id.search_box);
    _filterText.addTextChangedListener(filterTextWatcher);