我有一个ListView
,我希望它能够实现搜索功能:
以下是我的MainActivity
课程中的代码:
ListView list = (ListView) findViewById(R.id.listView1);
adapter = new LazyImageLoadAdapter(Companieslist.this, mStrings,company_name, company_website);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
originalValues = new ArrayList<HashMap<String,Object>>();
HashMap<String , Object> temp;
int nb = company_name.size();
for (int i = 0; i < nb; i++) {
temp = new HashMap<String, Object>();
temp.put("name", company_name.get(i));
temp.put("url", mStrings.get(i));
temp.put("website", company_website.get(i));
//add the row to the ArrayList
originalValues.add(temp);
}
searchResults = new ArrayList<HashMap<String,Object>>(originalValues);
searchlist.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Companieslist.this.adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
});
ListView
的自定义适配器如下:
//Adapter class extends with BaseAdapter and implements with OnClickListener
public class LazyImageLoadAdapter extends BaseAdapter implements OnClickListener {
private Activity activity;
private List<String> data,names,company_website;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private List<String>filteredData = null;
private List<String>filteredimages = null;
private ItemFilter mFilter = new ItemFilter();
public LazyImageLoadAdapter(Activity a, List<String> d,List<String> n, List<String> cw) {
activity = a;
data = d;
names = n;
this.filteredData = n ;
this.filteredimages = data ;
company_website=cw;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Create ImageLoader object to download and show image in list
// Call ImageLoader constructor to initialize FileCache
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;
}
// Create a holder Class to contain inflated xml file elements
public static class ViewHolder{
public TextView text;
// public TextView text1;
// public TextView textWide;
public ImageView image;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null) {
// Inflate tabitem.xml file for each row ( Defined below )
vi = inflater.inflate(R.layout.listview_row, null);
// View Holder Object to contain tabitem.xml file elements
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
// holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
// Set holder with LayoutInflater
vi.setTag(holder);
} else holder = (ViewHolder) vi.getTag();
holder.text.setText(names.get(position));
// holder.text1.setText("company description "+position);
ImageView image = holder.image;
// DisplayImage function from ImageLoader Class
imageLoader.DisplayImage(data.get(position), image);
holder.image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// v.getContext().startActivity(new Intent(v.getContext(),Register.class));
Toast.makeText(v.getContext(),company_website.get(position), Toast.LENGTH_LONG).show();
}
});
// Set Item Click Listner for LayoutInflater for each row
vi.setOnClickListener(new OnItemClickListener(position));
return vi;
}
@Override
public void onClick(View arg0) {}
// Called when Item click in ListView
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position) {
mPosition = position;
}
@Override
public void onClick(View arg0) {
Companieslist sct = (Companieslist)activity;
sct.onItemClick(mPosition);
}
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String>list = names;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (List<String>) results.values;
notifyDataSetChanged();
}
}
}
可以这么说,搜索功能不起作用。根本没有过滤,ListView
保持不变。有谁知道什么是错的?