Android 在阵列适配器中实现Filterable不会筛选任何结果-与筛选之前的列表相同
这是我第一次在代码中实现Filterable。我遵循了所有的步骤,但是过滤仍然没有过滤任何结果Android 在阵列适配器中实现Filterable不会筛选任何结果-与筛选之前的列表相同,android,android-edittext,android-arrayadapter,android-filterable,Android,Android Edittext,Android Arrayadapter,Android Filterable,这是我第一次在代码中实现Filterable。我遵循了所有的步骤,但是过滤仍然没有过滤任何结果 在我的阵列适配器中实现可筛选 根据阵列适配器中的名称创建我的筛选器 重写getFilter以使用我的新过滤器阵列适配器 将edittext链接到阵列适配器并请求筛选 我仍然看到与过滤前相同的列表。我错过了什么?请看下面我的代码。提前谢谢 public class OnlineShopsAdapter extends ArrayAdapter<OnlineShop> implements
- 在我的阵列适配器中实现可筛选
- 根据阵列适配器中的名称创建我的筛选器
- 重写getFilter以使用我的新过滤器阵列适配器
- 将edittext链接到阵列适配器并请求筛选
public class OnlineShopsAdapter extends ArrayAdapter<OnlineShop> implements Filterable {
private Context context;
private List<OnlineShop> mOnlineShops;
Filter mOnlineShopsFilter;
public OnlineShopsAdapter (Context context, int resource, List<OnlineShop> objects){
super(context, resource, objects);
this.context = context;
this.mOnlineShops = objects;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
final OnlineShop onlineShop = getItem(position);
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.one_online_shop_display, parent, false);
}
mOnlineShopsFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Toast.makeText(getContext(),constraint,Toast.LENGTH_SHORT).show();
FilterResults filterResults = new FilterResults();
ArrayList<OnlineShop> tempList=new ArrayList<OnlineShop>();
//constraint is the result from text you want to filter against.
//objects is your data set you will filter from
if(constraint != null && mOnlineShops!=null) {
int length=mOnlineShops.size();
int i=0;
while(i<length){
OnlineShop item=mOnlineShops.get(i);
//do whatever you wanna do here
//adding result set output array
if(item.getShopName().toUpperCase().contains(constraint.toString().toUpperCase() )) {
Toast.makeText(getContext(),"Im in",Toast.LENGTH_SHORT).show();
tempList.add(item);
}
i++;
}
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = tempList;
filterResults.count = tempList.size();
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
mOnlineShops = (ArrayList<OnlineShop>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return convertView;
}
@Override
public Filter getFilter() {
return mOnlineShopsFilter;
}
}
我的班级:
public class OnlineShop {
private String shopName;
private int preferenceScore;
public OnlineShop (){
}
public String setShopName(String shopName) {
this.shopName = shopName;
keywords.add(shopName);
return shopName;
}
public int setPreferenceScore(int preferenceScore) {
this.preferenceScore = preferenceScore;
return preferenceScore;
}
public String getShopName() { return shopName; }
public int getPreferenceScore() { return preferenceScore; }
public static Comparator<OnlineShop> OnlineShopPreferenceScoreComparator
= new Comparator<OnlineShop>() {
public int compare(OnlineShop onlineShop1, OnlineShop onlineShop2) {
int onlineShop1PreferenceScore1 = onlineShop1.getPreferenceScore();
int onlineShop1PreferenceScore2 = onlineShop2.getPreferenceScore();
//ascending order
//fruitName1.compareTo(fruitName2);
//descending order
//return fruitName2.compareTo(fruitName1);
return onlineShop1PreferenceScore1 - onlineShop1PreferenceScore2;
}
};
}
公共类在线商店{
私有字符串商店名称;
个人智力倾向得分;
公共网上商店(){
}
公共字符串setShopName(字符串shopName){
this.shopName=shopName;
关键词.添加(店名);
返回店名;
}
公共int-setPreferenceScore(int-preferenceScore){
this.preferenceScore=首选项得分;
返回偏好得分;
}
公共字符串getShopName(){return shopName;}
public int getPreferenceScore(){return preferenceScore;}
在线公共静态比较器ShopPreferenceScoreComparator
=新的比较器(){
公共整数比较(OnlineShop OnlineShop 1、OnlineShop OnlineShop 2){
int onlineShop1PreferenceScore1=onlineShop1.getPreferenceScore();
int onlineShop1PreferenceScore2=onlineShop2.getPreferenceScore();
//升序
//果名1.比较(果名2);
//降序
//返回果名2.compareTo(果名1);
返回OnlineShop1ReferencesCore1-OnlineShop1ReferencesCore2;
}
};
}
我还必须覆盖以下方法:
public int getCount() {
return mOnlineShops.size();
}
public OnlineShop getItem(int position) {
return mOnlineShops.get(position);
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return mOnlineShops.size();
}
public OnlineShop getItem(int position) {
return mOnlineShops.get(position);
}
public long getItemId(int position) {
return position;
}