如何在Android Spinner中添加搜索选项? [英] How to add search option to Android Spinner?
问题描述
我在android微调器中显示了一长串数据.所以我想向此微调器添加搜索选项?任何人都可以用一个简单的示例代码来帮助我.(我看到了一些与此相关的答案,但还不够)..
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)..
我是android新手,我知道实际上这不是正确的方法.但我想将这种选项添加到微调器中.当您在搜索框上点击一个字母时,微调器中会显示与该字母相关的项目列表.非常感谢.
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);
}
推荐答案
使用TextWatcher
,然后在适配器上调用notifyDataSetChanged()
:
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();
}
});
这篇关于如何在Android Spinner中添加搜索选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!