我的ListView中的搜索篩選器存在一些問題。當我將我的對象的字母輸入到搜索框中時,它會正常搜索。但是,當我添加其他任何東西時,儘管它不包含該文本,但這些項仍保留在ListView中。你可以在照片中看到它。ListView搜索篩選器會留下不受歡迎的數據
你有什麼想法,該如何解決?
適配器,帶過濾器類(如下圖):
public class AnimalAdapter extends ArrayAdapter<Animal> implements Filterable{
private Context mContext;
private List<Animal> mAnimals;
ImageLoader imageLoader;
DisplayImageOptions options;
Activity activity;
AnimalAdapter adapter;
private Filter animalFilter;
private List<Animal> animaly;
ListView mListView;
RelativeLayout row;
@SuppressWarnings("deprecation")
public AnimalAdapter(Context context, List<Animal> objects) {
super(context, R.layout.animal_row_item, objects);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.build();
this.mContext = context;
this.mAnimals = objects;
this.animaly = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.animal_row_item, null);
}
final Animal animal = mAnimals.get(position);
TextView animalView = (TextView) convertView.findViewById(R.id.animal_text);
TextView areaView = (TextView) convertView.findViewById(R.id.area_text);
final ImageView animalPic = (ImageView)convertView.findViewById(R.id.animal_pic);
final ProgressBar indicator = (ProgressBar)convertView.findViewById(R.id.progress);
indicator.setVisibility(View.VISIBLE);
animalPic.setVisibility(View.INVISIBLE);
//Setup a listener we can use to switch from the loading indicator to the Image once it's ready
ImageLoadingListener listener = new ImageLoadingListener(){
@Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
indicator.setVisibility(View.INVISIBLE);
animalPic.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String arg0, View view, FailReason arg2) {
}
};
imageLoader.displayImage(animal.getImgUrl(), animalPic,options, listener);
animalView.setText(animal.getAnimal());
areaView.setText(animal.getArea());
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), MoreActivity.class);
intent.putExtra("about", animal.getAbout());
intent.putExtra("animal", animal.getAnimal());
intent.putExtra("imgUrl", animal.getImgUrl());
getContext().startActivity(intent);
}
});
return convertView;
}
public int getCount() {
return mAnimals.size();
}
@Override
public Filter getFilter() {
if (animalFilter == null)
animalFilter = new AnimalFilter();
return animalFilter;
}
private class AnimalFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = animaly;
results.count = animaly.size();
}
if (constraint!= null && constraint.toString().length() > 0) {
List<Animal> nAnimalList = new ArrayList<Animal>();
for (Animal p : animaly) {
if (p.getAnimal().toUpperCase().contains(constraint.toString().toUpperCase())
&&p.getAnimal().toUpperCase().startsWith(constraint.toString().toUpperCase()))
nAnimalList.add(p);
if (p.getAnimal().toUpperCase().contains(constraint.toString().toUpperCase())
&&!p.getAnimal().toUpperCase().startsWith(constraint.toString().toUpperCase()))
nAnimalList.remove(p);
}
results.values = nAnimalList;
results.count = nAnimalList.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// Now we have to inform the adapter about the new list filtered
if (results.count == 0)
notifyDataSetInvalidated();
else {
mAnimals = (List<Animal>) results.values;
notifyDataSetChanged();
}
}
}
}
偏題,但我喜歡可愛的小貓照片:3 – FWeigl
lol hahaha。不錯的選擇吧? – marson