How to add search parameter in Android Spinner?

I have a long list of data mapping in android spinner. So, I want to add a search parameter to this counter? Can someone help me with a simple code example .. (I saw some answers regarding this, but they are not enough).

I am new to android and I know that it is not. but I want to add this option to the counter. When you click a letter in the search box, the list of items is displayed in the line corresponding to this letter. Many thanks.

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); } 
+6
source share
1 answer

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(); } }); 
+1
source

Source: https://habr.com/ru/post/914494/


All Articles