在 Array Adapter 中实现 Filterable 不会过滤任何结果 - 与过滤前相同的列表
Posted
技术标签:
【中文标题】在 Array Adapter 中实现 Filterable 不会过滤任何结果 - 与过滤前相同的列表【英文标题】:Implementing Filterable in Array Adapter is not filtering any results - same list as before filtering 【发布时间】:2018-10-13 21:33:49 【问题描述】:这是我第一次在我的代码中实现 Filterable。我遵循了所有步骤,但过滤仍然没有过滤任何结果。
在我的阵列适配器中实现了可过滤 根据数组适配器中的名称制作我的过滤器 覆盖 getFilter 以使用我的新过滤器阵列适配器 将编辑文本链接到数组适配器并请求过滤我仍然看到与过滤前相同的列表。我错过了什么?请在下面查看我的代码。提前致谢!
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;
这是我的编辑文本所在的位置:
private GridView mOnlineShopsListView;
private ArrayList searchResultsKeysForShops;
@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View v = inflater.inflate(R.layout.get_points,container,false);
mOnlineShopsListView = (GridView) v.findViewById(R.id.online_shops_grid_view);
//making the adapter work so it can show the Online Shops from the database
mOnlineShopsListView.setAdapter(MainActivity.mOnlineShopsAdapter);
editSearch.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
mOnlineShopsAdapter.getFilter().filter(s);
@Override
public void afterTextChanged(Editable s)
);
return v;
和我的班级:
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;
;
【问题讨论】:
【参考方案1】:我还必须重写以下方法:
public int getCount()
return mOnlineShops.size();
public OnlineShop getItem(int position)
return mOnlineShops.get(position);
public long getItemId(int position)
return position;
【讨论】:
以上是关于在 Array Adapter 中实现 Filterable 不会过滤任何结果 - 与过滤前相同的列表的主要内容,如果未能解决你的问题,请参考以下文章
我将如何在播放框架 2.4.3 (Scala) 中实现拦截器/过滤器
Swift中实现Array数组和NSArray数组的相互转换与遍历