Android CardListView过滤器

Android CardListView过滤器,android,listview,android-filter,Android,Listview,Android Filter,我正在使用来自的CardListView。 因此,假设我有一个带有适配器的简单cardListView CardListView cardListView = (CardListView) findViewById(R.id.card_list); ArrayList cards = new ArrayList<>(); Card card = new Card(this); card.setTitle("card text"); CardHeader header = new C

我正在使用来自的CardListView。 因此,假设我有一个带有适配器的简单cardListView

CardListView cardListView = (CardListView) findViewById(R.id.card_list);
ArrayList cards = new ArrayList<>();

Card card = new Card(this);
card.setTitle("card text");
CardHeader header = new CardHeader(this);
header.setTitle("card header");
card.addCardHeader(header);

cards.add(card);
CardArrayAdapter adapter = new CardArrayAdapter(this, cards);
cardListView.setAdapter(adapter);
但我不知道它是如何过滤卡片的。就我而言,我把它放在

@Override
public boolean onQueryTextChange(String s) {
    adapter.getFilter().filter(s);
    return true;
}
但是它在我的列表中找不到任何卡片,无论是通过我在card.setTitle()中设置的文本还是通过header.setTitle()

有人知道这是怎么回事吗?
非常感谢您抽出时间分享您的想法。

正如我在源代码中看到的,该库没有自定义过滤器的实现,因此首先您必须实现类似于此的自定义过滤器:

Filter cardFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();   
        ArrayList<Card> tempList = new ArrayList<Card>();
        // Where constraint is the value you're filtering against and
        // cards is the original list of elements
        if(constraint != null && cards!=null) {
            // Iterate over the cards list and add the wanted
            // items to the tempList

            filterResults.values = tempList;
            filterResults.count = tempList.size();
        }
        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        // Update your adapter here and notify
        cardArrayAdapter.addAll(results.values);
        cardArrayAdapter.notifyDataSetChanged();
    }
};
Filter cardFilter=new Filter(){
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults FilterResults=新的FilterResults();
ArrayList tempList=新的ArrayList();
//其中constraint是要过滤的值,并且
//卡片是元素的原始列表
if(约束!=null&&cards!=null){
//迭代卡片列表并添加想要的卡片
//圣殿骑士的物品
filterResults.values=模板列表;
filterResults.count=templast.size();
}
返回过滤器结果;
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
//在此处更新适配器并通知
cardarayadapter.addAll(results.values);
notifyDataSetChanged();
}
};
在此之后,就我所知,您有两个选择:

1) 修改库源代码并覆盖
CardArrayAdapter
BaseCardArrayAdapter
中的
getFilter()
方法,以返回
customFilter

2) 直接在代码中实现过滤逻辑,并且仅在更新
onquerytexchanged
中的文本时才更新适配器


在这里,您可以找到代码的参考:

arrayAdapter实现了Filterable。例如,它可以很好地处理字符串或int。 在您的情况下,您使用的是一张卡片。 在我看来,首先应该覆盖卡中的toString()方法

ArrayAdapter中的默认getFilter()方法使用object.toString()过滤列表

如果没有足够的过滤器,则必须实现自定义过滤器

问候,,
Javier。

编辑您的答案,至少包含您发布的链接的重要部分。链接将无法在几个月内运行,您的答案将变得无用。
Filter cardFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();   
        ArrayList<Card> tempList = new ArrayList<Card>();
        // Where constraint is the value you're filtering against and
        // cards is the original list of elements
        if(constraint != null && cards!=null) {
            // Iterate over the cards list and add the wanted
            // items to the tempList

            filterResults.values = tempList;
            filterResults.count = tempList.size();
        }
        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        // Update your adapter here and notify
        cardArrayAdapter.addAll(results.values);
        cardArrayAdapter.notifyDataSetChanged();
    }
};