对于我的自定义适配器,我希望能够过滤 ListView 中的项目。我实现了Filterable,并创建了一个Filter。
这是我的发布结果:
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
MainActivity.this.filteredPetList.clear();
MainActivity.this.filteredPetList.addAll((ArrayList<Pet>)results.values);
notifyDataSetChanged();
}
如果我在之后检查filteredPetList
MainActivity.this.filteredPetList.addAll((ArrayList<Pet>)results.values);
,结果显示很好,但适配器不会更新视图。
任何想法我做错了什么?我可能又在做一些愚蠢的事情了。
-Edit- 在我的适配器中,我覆盖了 notifyDataSetChanged,以查看它是否实际被调用。它说:
@Override
public void notifyDataSetChanged()
{
System.out.println("Notified...");
super.notifyDataSetChanged();
}
每当我尝试过滤时,都会得到一个结果,notifyDataSetChanged 会被调用。我还检查了我的 filteredPetList 是否真的在过滤器之外被更改,并且确实如此。
只是视图由于某种原因没有更新......
-Edit- 添加了完整的适配器代码:
private class PetAdapter extends BaseAdapter implements Filterable
{
private final Object lock = new Object();
List<Row> rows;
ArrayList<Integer> petIds;
boolean clickable;
PetFilter petFilter;
public PetAdapter(ArrayList<Pet> petList, boolean clickable)
{
this.clickable = clickable;
rows = new ArrayList<Row>();
this.petIds= new ArrayList<Integer>();
for(Pet p : petList)
{
rows.add(new ImageTextTextRow(LayoutInflater.from(MainActivity.this), p.getPetImageId(), p.getPetName(), p.getPetFood()));
petIds.add(p.getPetId());
}
}
@Override
public boolean isEnabled(int position)
{
//Can do that certain items ARE enabled by using the position
return clickable;
}
@Override
public int getViewTypeCount()
{
return RowType.values().length;
}
@Override
public int getItemViewType(int position)
{
return rows.get(position).getViewType();
}
@Override
public int getCount()
{
return rows.size();
}
@Override
public Object getItem(int position)
{
return rows.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return rows.get(position).getView(convertView);
}
/**
*
* @param position
* @return
*/
public int getSelectedPetId(int position)
{
return petIds.get(position);
}
@Override
public Filter getFilter()
{
if (petFilter == null)
petFilter = new PetFilter();
return petFilter;
}
private class PetFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
if (MainActivity.this.filteredPetList == null)
{
synchronized (PetAdapter.this.lock)
{
MainActivity.this.filteredPetList = new ArrayList<Pet>(MainActivity.this.originalPetList);
}
}
if (constraint == null || constraint.length() == 0)
{
synchronized (PetAdapter.this.lock)
{
results.values = MainActivity.this.originalPetList;
results.count = MainActivity.this.originalPetList.size();
}
}
else
{
String constraintString = constraint.toString().toLowerCase(Locale.ENGLISH);
final ArrayList<Pet> items = MainActivity.this.filteredPetList;
final int count = items.size();
final ArrayList<Pet> newItems = new ArrayList<Pet>(count);
for (int i = 0; i < count; i++)
{
final Pet item = items.get(i);
final String itemName = item.getPetName().toLowerCase(Locale.ENGLISH);
if (itemName.startsWith(constraintString))
{
newItems.add(item);
}
else
{
final String[] words = itemName.split(" ");
final int wordCount = words.length;
for (int k = 0; k < wordCount; k++)
{
if (words[k].startsWith(constraintString))
{
newItems.add(item);
break;
}
}
}
}
results.values = newItems;
results.count = newItems.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
MainActivity.this.filteredPetList.clear();
MainActivity.this.filteredPetList.addAll((ArrayList<Pet>)results.values);
if (results.count > 0)
PetAdapter.this.notifyDataSetChanged();
else
PetAdapter.this.notifyDataSetInvalidated();
}
}
}
提前致谢!
特里根