【问题标题】:Filtering LIstview that extends LIst Activity过滤扩展 LIst Activity 的 LIstview
【发布时间】:2012-12-28 18:57:26
【问题描述】:

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

【问题讨论】:

    标签: android listview filter


    【解决方案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) {
    
            }
    

    【讨论】:

    • 哎呀!每次在 EditText 中添加或删除一个字母时,无需使用两次findViewById()。为什么要使用setAdapter() 将 ListView 传递给它的当前适配器? (抱歉,leenolasco 的代码比这个建议要快。)
    • 是的,我再次检查了,你是对的。 findViewById() 和 setAdapter() 是不必要的。
    【解决方案2】:

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

    ArrayAdapter<String> _myAdapter;
    

    并像这样在onCreate() 中使用它:

    _myAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, result);
    setListAdapter(_myAdapter);
    

    【讨论】:

    • 感谢先生,它现在可以工作了!!我如何使这个搜索成为单词之间的字母?
    • “我如何使这个搜索成为一个介于单词之间的字母?”我不明白。可以举个例子吗?
    • 如果我按“A”,列表将返回所有以字母 A 开头的单词。我要求先生的是,当按 A 时,它会返回包含字符序列的单词,即使它是词之间。示例如果我按 A,L 它将分别返回带有 a 和 L 的单词
    • 因此,如果您按“al”,您希望看到“Aloof”、“Bald”、“Canal”等词?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多