user833178
user833178

Reputation: 21

Search on listview (activity extends ListActivity)

i extend ListActivity and bind ListView in my activity. applied filter to listview. the filter is working fine when use emulater, because i have keyboard of my laptop. but the problem arises when i want to filter data on android because it does not display keyboard to me. code is give below.

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

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

Upvotes: 1

Views: 2128

Answers (1)

Zoleas
Zoleas

Reputation: 4879

Put an EditText somewhere, like on top of your listView, then add a TextWatcher like :

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

Upvotes: 3

Related Questions