【问题标题】:Filter listview筛选列表视图
【发布时间】:2012-10-01 17:48:06
【问题描述】:

这是我的过滤器的代码:

private class NameFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        // NOTE: this function is *always* called from a background thread,
        // and
        // not the UI thread.
        constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if (constraint != null && constraint.toString().length() > 0) {
            ArrayList<Place> filt = new ArrayList<Place>();
            ArrayList<Place> lItems = new ArrayList<Place>();
            synchronized (this) {
                lItems.addAll(objects);
            }
            for (int i = 0, l = lItems.size(); i < l; i++) {
                Place m = lItems.get(i);
                if (m.getName().toLowerCase().contains(constraint))
                    filt.add(m);
            }
            result.count = filt.size();
            result.values = filt;
        } 
        else {
            synchronized (this) {
                result.values = objects;
                result.count = objects.size();
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        // NOTE: this function is *always* called from the UI thread.
        filtered = (ArrayList<Place>) results.values;
        notifyDataSetChanged();
        clear();
        for (int i = 0, l = filtered.size(); i < l; i++)
            add(filtered.get(i));
        notifyDataSetInvalidated();
    }

}

这是我的活动代码:

lvPlace = (ListView) findViewById(R.id.listView1);

    final ArrayList<Place> searchResults = GetSearchResults();

    filterEditText = (EditText) findViewById(R.id.filter_text);
    filterEditText.addTextChangedListener(filterTextWatcher);

    adapter = new PlaceAdapter(this, 0, searchResults);

    lvPlace.setAdapter(adapter);
    lvPlace.requestFocus();
    lvPlace.setTextFilterEnabled(true);

private 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) {
        if (adapter != null) {
            adapter.getFilter().filter(s.toString().toLowerCase());
            filterEditText.getText().toString();
        } else {
            Log.d("filter", "no filter availible");
        }
    }

};

@Override
protected void onDestroy() {
    super.onDestroy();
    filterEditText.removeTextChangedListener(filterTextWatcher);
}

我已经为此苦苦挣扎了一段时间,因为当我在搜索框中输入内容时它工作正常,但是一旦我从搜索字段中删除了文本,列表就不会返回到初始状态. 请帮我解决这个问题!

【问题讨论】:

    标签: android listview filter


    【解决方案1】:

    在你的 publishResults 方法中试试这个:

    protected void publishResults(CharSequence constraint,FilterResults results)
    {   
        filtered.clear();
        filtered = (ArrayList<Place>) results.values;
        if(filtered.size() < 1)
            filtered = objects;
    
        notifyDataSetChanged();
    
        for (int i = 0, l = filtered.size(); i < l; i++)
            add(filtered.get(i));
        notifyDataSetInvalidated();
    }
    

    使用此代码,您可以在过滤方法中取出 else 子句。

    另外,您还可以通过使用这样的循环使您的 for 循环更高效:

    for(Place place : filtered)
        add(place);
    

    【讨论】:

      【解决方案2】:

      当我在搜索框中输入内容时,它工作正常,但是 一旦我从搜索字段列表中删除了文本,就没有返回 初始状态。

      如果没有看到适配器的代码,很可能会发生这种情况,因为您没有保留对适配器使用的初始值的引用。例如,如果您将带有数据的ArrayList 设置到适配器中并开始在过滤EditText 中输入字符,这将起作用,因为通过过滤您仅使用前一组值的子集(这将始终可用)。当您从 EditText 中删除一个字符时,您需要对前一组值进行过滤,但随着该组被替换,这些值不再存在并且过滤中断。

      解决方案是制作初始值的副本,以始终拥有完整的ArrayList 值来进行过滤。适配器将使用ArrayList 的一份副本(这将是ArrayList 将在publishResults 中替换为过滤结果),另一份将用于由过滤器进行过滤(这ArrayList 将保持不变!)在performFiltering 方法中。

      【讨论】:

      • @HùngPhiCao 你解决问题了吗?如果没有添加您使用的适配器的代码。
      猜你喜欢
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多