使用ArrayAdapter在Android中自定义过滤我正在尝试过滤使用此ArrayAdapter填充的ListView:package me.alxandr.android.mymir.adapters;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.Set;import me.alxandr.android.mymir.R;import me.alxandr.android.mymir.model.Manga;import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.Filter;import android.widget.SectionIndexer;import android.widget.TextView;public class MangaListAdapter extends ArrayAdapter<Manga> implements SectionIndexer{
public ArrayList<Manga> items;
public ArrayList<Manga> filtered;
private Context context;
private HashMap<String, Integer> alphaIndexer;
private String[] sections = new String[0];
private Filter filter;
private boolean enableSections;
public MangaListAdapter(Context context, int textViewResourceId, ArrayList<Manga> items, boolean enableSections)
{
super(context, textViewResourceId, items);
this.filtered = items;
this.items = filtered;
this.context = context;
this.filter = new MangaNameFilter();
this.enableSections = enableSections;
if(enableSections)
{
alphaIndexer = new HashMap<String, Integer>();
for(int i = items.size() - 1; i >= 0; i--)
{
Manga element = items.get(i);
String firstChar = element.getName().substring(0, 1).toUpperCase();
if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A')
firstChar = "@";
alphaIndexer.put(firstChar, i);
}
}
}但是,当我在过滤器上调用filter('test')时根本没有任何事情发生(或者运行后台线程,但是只要用户认可,列表就不会被过滤)。我怎样才能解决这个问题?
3 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
我觉得不需要做任何上述事情。覆盖POJO的toString()方法,如...
public String toString() { return this.getCallTitle() == null ? "" : this.getCallTitle().toLowerCase();}
TextWatcher只是过滤和更简单的方法。
adapter.getFilter().filter(chars.toString().toLowerCase());
除非您有自定义过滤,否则此解决方案有效。希望这对别人有益。
- 3 回答
- 0 关注
- 616 浏览
添加回答
举报
0/150
提交
取消