我有一个使用数组适配器的ListView:
//global
ArrayList<Location> locations=new ArrayList<Location>(); //array list of location objects
ArrayAdapter<Location> theLocations;
ListView listView;
then in onCreateView of my Fragment:
theLocations = new ArrayAdapter<Location>(mContext,
R.layout.location_row, locations);
listView.setAdapter(theLocations);
我认为这工作得很好,虽然如果我尝试更新这个适配器,它会崩溃…
//in my fragment, another dialog makes this call here to update the list.
public void onUIUpdate(Location l) { //listener call back when dialog "ok" clicked
locations.add(l); //it is okay with this.
theLocations.notifyDataSetChanged(); //dies
}
它以错误结束:
07-23 08:03:28.655: ERROR/AndroidRuntime(509): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
是否意味着它不喜欢ArrayAdapter中的Location ??这是否意味着我必须制作自己的自定义列表适配器?如果我不调用thellocations . notifydatasetchanged()一切都很好,但没有更新…(
我应该注意到Location类中的toString()被重写以返回String名称;
在注释之后,我尝试了这个:
public void onUIUpdate(Location l) {
locations.add(l);
//theLocations.notifyDataSetChanged();
WrapperListAdapter wr = (WrapperListAdapter)listView.getAdapter();
ArrayAdapter aa=(ArrayAdapter)wr.getWrappedAdapter();
aa.notifyDataSetChanged();
}
哈哈,运气不好,我是瞎猜的。我还在想是不是我在ListView上使用的addHeaderView ?我不确定它是否使它不可变在未来使用addHeaderView?
我不会评论ListView的设计,它是非常丑陋的,但我做ListView . getadapter ().getWrappedAdapter().notifyDataSetChabged();