'How to add search option to Android Spinner?
I have a long list of data display in android spinner. So i want to add a search option to this spinner ? Can any one help me with a simple example code.. (I saw some answers regarding this but they are not sufficient)..
I am new to android and i know actually this is not the correct way. but i want to add this kind of option to the spinner. When you hit a letter on the search box , list of items are displayed in the spinner relevant to that letter. Thanks a lot.
public void search(View view){
cursor = db.rawQuery("SELECT * FROM tblRepTeritories WHERE RepCode like?",
new String[]{"%" + searchText.getText().toString() + "%"});
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
cursor,
new String[] {"TeriCode"},
new int[] {android.R.id.text1});
adapter1.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
Spinner s1 = (Spinner) findViewById( R.id.spinner2 );
s1.setAdapter(adapter1);
}
Solution 1:[1]
Use TextWatcher and then call notifyDataSetChanged() on your adapter:
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
cursor = db.rawQuery("SELECT * FROM tblRepTeritories WHERE RepCode like?",
new String[] {"%" + searchText.getText().toString() + "%"});
adapter1.notifyDataSetChanged();
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jared Rummler |
