[英]Filter a Listview using a spinner in android
I have this Listview that contains both text and images. 我有既包含文本又包含图像的Listview。 Each list item contains four key elements.
每个列表项包含四个关键元素。 I want to be able to filter the listview using either of the three TextViews by an option selected from the spinner.
我希望能够通过从微调器中选择的一个选项,使用三个TextViews之一来过滤listview。 This is what I have done so far:
到目前为止,这是我所做的:
public class LazyVenueAdapter extends BaseAdapter implements Filterable {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyVenueAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.venue_list_item, null);
TextView title = (TextView) vi.findViewById(R.id.venueName);
TextView location = (TextView) vi.findViewById(R.id.venueLocation);
TextView tags = (TextView) vi.findViewById(R.id.venueTags);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.venueImage);
HashMap<String, String> venue = new HashMap<String, String>();
venue = data.get(position);
// Setting all values in listview
title.setText(venue.get(VenuesFragment.KEY_TITLE));
location.setText(venue.get(VenuesFragment.KEY_LOCATION));
tags.setText(venue.get(VenuesFragment.KEY_TAGS));
imageLoader.DisplayImage(venue.get(VenuesFragment.KEY_THUMB_URL),
thumb_image);
return vi;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<HashMap<String, String>> filteredArrayVenues = new ArrayList<HashMap<String, String>>();
data.clear();
if (constraint == null || constraint.length() == 0) {
results.count = data.size();
results.values = data;
} else {
constraint = constraint.toString();
for (int index = 0; index < data.size(); index++) {
HashMap<String, String> dataVenues = data.get(index);
if (dataVenues.get(VenuesFragment.KEY_TAGS).toString().startsWith(
constraint.toString())) {
filteredArrayVenues.add(dataVenues);
}
}
results.count = filteredArrayVenues.size();
System.out.println(results.count);
results.values = filteredArrayVenues;
Log.e("VALUES", results.values.toString());
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
data = (ArrayList<HashMap<String, String>>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
The problem that I am having is that when I select an option from the drop down list I get back an empty result set. 我遇到的问题是,当我从下拉列表中选择一个选项时,我会得到一个空的结果集。
You don't receive any results because you delete the information that you are trying to build your results from with data.clear();
您没有收到任何结果,因为您使用
data.clear();
删除了您试图从中构建结果的信息data.clear();
. 。 If
data
is now empty (ie data.size() == 0
) then this loop will never execute: 如果
data
现在为空(即data.size() == 0
),则此循环将永远不会执行:
for (int index = 0; index < data.size(); index++) {
Removing this line might solve your problem: 删除此行可能会解决您的问题:
data.clear();
您必须先过滤ArrayList<HashMap<String, String>> data
,然后在adapter
调用notifyDataSetChanged()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.