【问题标题】:How to filter a filtered ListView, using SimpleAdapter如何使用 SimpleAdapter 过滤过滤后的 ListView
【发布时间】:2013-11-07 00:34:09
【问题描述】:

我整天都在做这个,但我没有得到任何结果!

我一直在搜索“多个过滤器”之类的帖子以及更多内容,但没有任何问题。

我有一个 SimpleAdapter 和一个填充的 ListView。现在我正在根据单词进行自定义搜索。我将单词与空格分开。例如:

想象一下,我的数据是:

猫喜欢狗

狗喜欢老鼠

老鼠喜欢奶酪

猫吃奶酪

如果我过滤“猫”,我将得到下一个列表:

猫喜欢狗

猫吃奶酪

如果我再次用“奶酪”过滤,我会得到:

猫吃奶酪

但是,我得到了这个:

老鼠喜欢奶酪

猫吃奶酪

所以,我只得到最后一个过滤器。

这是我的代码:

public void onTextChanged(CharSequence cs, int arg1, int arg2,int arg3) {
String[] separated = (cs).split(" +"); 
// With conditions not metioned here, I'm supposed to enter only words and spaces like the examples
for (int i2 = 0; i2 < separated.length ; i2++)  {
    adapter3.getFilter().filter(separated[i2]);
    adapter3.notifyDataSetChanged();                            
    
}

}

提前致谢!!!


编辑:

我得到了我的 CustomAdapter。我可以看到我的结果,但我无法设置我的适配器,不知道为什么。 因此,当我在另一个函数上创建 setAdapter(adapter) 时,它不会显示我。 这是我的适配器:

    public class Adaptador extends SimpleAdapter implements Filterable {

    public Adaptador(Context context, ArrayList<HashMap<String, String>> data,
            int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public Filter getFilter() {
        filtrodoble fil = new filtrodoble();
        return fil;
    }
    
    public class filtrodoble extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // TODO Auto-generated method stub
            FilterResults Result = new FilterResults();
            // if constraint is empty return the original names
            ArrayList<String> Filtered_Names = new ArrayList<String>();
            Filtered_Names = arraylista;
            if(constraint.length() == 0 ){
                Result.values = Filtered_Names;
                Result.count = Filtered_Names.size();
                return Result;
            }
            String nomestringa = String.valueOf(constraint);
            String[] separated = nomestringa.split(" +");

            for (int i=0;i<separated.length;i++){

            Filtered_Names = doSomethingVoid(Filtered_Names, String.valueOf(separated[i])); // it returns another ArrayList<string>
            }

            
            for (int i2 = 3; i2<Filtered_Names.size(); i2=i2+6)
            
            
            Result.values = Filtered_Names;
            Result.count = Filtered_Names.size();
            return Result;
            
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {

            try{
                arrayaux=null;
            arrayaux = (ArrayList<String>)results.values; // MY RESULTS ARE GREAT HERE
            notifyDataSetChanged();
        
            } catch (Exception e){
                e.printStackTrace();
                notifyDataSetInvalidated();
            }
        }

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        return convertView;
      } 
}

已解决!!

由于 OnTextChanged 的​​参数,我有很多 ot lv.setAdapter(null) ......然后其中一些被missclick或其他东西改变了。对不起!

;)

【问题讨论】:

  • 也试过getApplicationContext().this.adapter3.getFilter().filter(separated[i2]);

标签: android listview filter simpleadapter


【解决方案1】:

我认为您无法使用内置过滤器做到这一点。您需要扩展 SimpleAdapter 并编写自己的 Filter 类。 Here's 一个帖子会告诉你如何做到这一点。

【讨论】:

  • 感谢您的回复,但我还是不明白。我有点初学者,所以我很抱歉。我不知道如何创建自定义过滤器...有好的教程吗?我已经访问了很多,但我不明白有人喜欢我的例子。
  • 首先您需要了解ListViewAdapterFilter 的工作原理。谷歌的条款,阅读 Android 文档和一些教程。 Google for "android listview custom adapter" 或 "android listview custom filter" 等。This 是一个很好的关于 ListView 和自定义适配器的教程。 here 是关于自定义过滤器的。
  • 我做到了,我确实知道 ListView、Adapter 和 Filter 是如何工作的,但只有一个 CharSequence 我之前说过,我没有找到一个很好的例子来理解 CustomAdapter 是如何工作的工作。
  • 我之前在评论中为您提供链接的教程(来自 Lars Vogel 的教程)有什么问题?它展示了如何创建自定义适配器非常好。拥有自定义适配器后,您可以添加自己的过滤器,如第二个教程所示。您将只有一个过滤器。在此过滤器中,您可以自己定义要显示的内容和不显示的内容。您可以根据需要过滤任意数量的不同字符串。
  • 嘿,我明白了,但现在适配器没有显示 setAdapter...我要编辑我的问题。
猜你喜欢
  • 1970-01-01
  • 2013-07-18
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
  • 2021-11-04
  • 1970-01-01
相关资源
最近更新 更多