我一直在阅读有关过滤列表视图的一些主题,但我感到困惑,因为我对适配器及其不同类型的知识很少。我找到了一个与我的问题几乎相似的解决方案。这是Zoleas发布的代码
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);
我很困惑如何在Ontextchanged方法中声明我的适配器,因为我的适配器看起来像这样。
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result));
在这里需要关于如何实现过滤器的帮助是我的listview上的完整代码。
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.firstaid);
filterText = (EditText) findViewById(R.id.acET);
DbHelper tblFa = new DbHelper(this);
tblFa.open();
ArrayList<String> result = tblFa.getFAData();
result = tblFa.getFAData();
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
tblFa.close();
}
答案 0 :(得分:1)
您只需创建另一个字段变量,就像filterText
:
ArrayAdapter<String> _myAdapter;
并在onCreate()
中使用它:
_myAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result);
setListAdapter(_myAdapter);
答案 1 :(得分:1)
试试;
EditText edt = (EditText) this.view.findViewById(R.id.editTxtView);
edt.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ListView listView = (ListView) view.findViewById(R.id.yourListviewID);
YourAdapterOrWhateverAdapter adapter = (YourAdapterOrWhateverAdapter) listView.getAdapter();
adapter.getFilter().filter(s);
@Override
public void afterTextChanged(Editable s) {
}