作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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();
}
}
}
}
最佳答案
虽然我认为您填充列表的逻辑可能已关闭(即无需在开始为空的列表上执行 .remove() ),但您的错误可能出现在publishResults中:
if (results.count == 0)
notifyDataSetInvalidated(); // <-- this isn't right
else
{
mAnimals = (List<Animal>) results.values;
notifyDataSetChanged();
}
notifyDataSetInvalidated() 可能不是您想要清空列表的方法,请改为这样做:
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
// even if results.values is an empty List<Animal>, you want to notify your adapter!
mAnimals = (List<Animal>) results.values;
notifyDataSetChanged();
}
关于java - ListView 搜索过滤器留下不受欢迎的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982077/
我是一名优秀的程序员,十分优秀!