我写了一个像这样的自己的 ArrayAdapter:
public class PoiListAdapter extends ArrayAdapter<Poi> implements Filterable {
private Context context;
private final List<Poi> valuesPoi;
private ItemsFilter mFilter;
public PoiListAdapter(Context context, List<Poi> valuesPoi) {
super(context, R.layout.poilist);
this.context = context;
this.valuesPoi = valuesPoi;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.poilist, parent, false);
TextView textViewName = (TextView) rowView.findViewById(R.id.name_poi);
TextView textViewDis = (TextView) rowView
.findViewById(R.id.discrip_poi);
textViewName.setText(valuesPoi.get(position).getName());
textViewDis.setText(valuesPoi.get(position).getDiscription());
return rowView;
}
/**
* Implementing the Filterable interface.
*/
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemsFilter(this);
}
return mFilter;
}
public List<Poi> getValuesPoi() {
return valuesPoi;
}
public void addValuesPoi(Poi p) {
valuesPoi.add(p);
}
@Override
public void clear() {
valuesPoi.clear();
}
}
对于这个适配器,我想实现一个搜索功能。因此我实现了一个自定义过滤器类:
public class ItemsFilter extends Filter {
private PoiListAdapter poiListAdapter;
public ItemsFilter(PoiListAdapter poiListAdapter) {
this.poiListAdapter = poiListAdapter;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
ArrayList<Poi> filterList = new ArrayList<Poi>();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<Poi> orginalList = new ArrayList<Poi>(
poiListAdapter.getValuesPoi());
for (Poi p : orginalList) {
if (p.getName().toLowerCase().contains(constraint))
filterList.add(p);
}
Log.i("DEBUG", orginalList.toString());
result.values = filterList;
result.count = filterList.size();
} else {
result.values = poiListAdapter.getValuesPoi();
result.count = poiListAdapter.getValuesPoi().size();
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Poi> fitems = (ArrayList<Poi>) results.values;
poiListAdapter.clear();
for (Poi p : fitems) {
poiListAdapter.addValuesPoi(p);
poiListAdapter.notifyDataSetChanged();
}
}
1. 问题
....我有一个 java.util.concurrentmodificationexception 用于:
for (Poi p : fitems) {
poiListAdapter.addValuesPoi(p);
poiListAdapter.notifyDataSetChanged();
}
我认为问题在于我想修改访问权限下的 Arraylist。我想我必须使用同步,但我以前从未使用过它。
更新: 这个问题解决了!这里的代码:
for(Iterator<Poi> i = fitems.iterator(); i.hasNext();) {
Poi p = i.next();
poiListAdapter.addValuesPoi(p);
//poiListAdapter.notifyDataSetChanged();
}
2.问题
列表视图在开始时是空的。一开始我想展示所有元素!通过搜索元素也不会显示任何内容!Listview 目前什么也没显示!