-1

我一直在阅读一些有关过滤列表视图的主题,但我感到困惑,因为我对适配器和不同种类的适配器知之甚少。我找到了一个与我的问题几乎相似的解决方案。这是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();
    }
4

2 回答 2

1

您只需要创建另一个字段变量,就像filterText

ArrayAdapter<String> _myAdapter;

并像这样使用它onCreate()

_myAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, result);
setListAdapter(_myAdapter);
于 2012-12-01T20:09:18.883 回答
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) {

        }
于 2012-12-01T20:25:15.297 回答