ListView with searching feature is very common in applications. You just need an EditText at the top of the ListView or as per your designs.
Below are the steps-
First you need to initialize the ListView and EditText
countryCodelistView = (ListView) findViewById(R.id.country_code_list_view);
countryCodeSearchEditText = (EditText) findViewById(R.id.country_code_search);
Now add ArrayAdapter in a ListView
adapter = new ArrayAdapter<String>(this, R.layout.country_code_list_item,
R.id.country_code_text_view, array);
countryCodelistView.setAdapter(adapter);
Here array is the list that holds by a ListView
Now final step is to apply addTextChangedListener on the EditText
countryCodeSearchEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
CountryCodeActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
3 Comment(s)