リストビューのフィルタリングに関するいくつかのトピックを読んでいますが、アダプターとさまざまな種類のアダプターに関する知識がほとんどないため、混乱しています。私の問題にほぼ似た解決策を見つけました。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));
ここでフィルタを実装する方法について助けが必要なのは、リストビューの完全なコードです。
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();
}