Android getView()(对于自定义ListView)不';notifyDatasetChanged()上未被调用
我有以下问题,搜索了一段时间,但没有从网上得到任何解决方案: 我有一个自定义列表视图,每个项目都有以下布局(我只发布了基本内容): 在main活动中,我有一个自定义listview:lvMainListView,带有一个自定义适配器(其类扩展了ArrayAdapter,并且偏离了路线:覆盖方法getView),适配器的数据集是:Android getView()(对于自定义ListView)不';notifyDatasetChanged()上未被调用,android,Android,我有以下问题,搜索了一段时间,但没有从网上得到任何解决方案: 我有一个自定义列表视图,每个项目都有以下布局(我只发布了基本内容): 在main活动中,我有一个自定义listview:lvMainListView,带有一个自定义适配器(其类扩展了ArrayAdapter,并且偏离了路线:覆盖方法getView),适配器的数据集是:ArrayList friends: private class FriendRowAdapter extends ArrayAdapter<Friend>
ArrayList friends
:
private class FriendRowAdapter extends ArrayAdapter<Friend> {
public FriendRowAdapter(Context applicationContext, int friendlistRow,
ArrayList<Friend> friends) {
super(applicationContext, friendlistRow, friends);
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
Friend friend = getItem(position);
FriendRowItem row = (FriendRowItem) convertView;
if ( row == null ) {
row = new FriendRowItem(ShowFriendsList.this.getApplicationContext());
}
row.setPropeties( friend );
return row;
}
}
私有类FriendRowAdapter扩展了ArrayAdapter{
公共FriendRowAdapter(上下文应用程序上下文,int friendlistRow,
ArrayList朋友){
超级(应用程序上下文、friendlistRow、friends);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
Friend=getItem(位置);
FriendRowItem行=(FriendRowItem)convertView;
if(行==null){
行=新的FriendRowItem(ShowFriendsList.this.getApplicationContext());
}
row.SetProperties(朋友);
返回行;
}
}
- 问题是,当我将朋友的状态从脱机更改为联机,然后调用notifyDataSetChanged(),什么都不会发生:该朋友的状态图标不会更改。
我试着调试,看到了代码:notifyDataSetChanged()被调用,但是定制的getView()没有被触发李>
儿子对不起,
我刚刚发现:因为我在另一个线程中调用了notifyDatasetChanged()方法(事实上,我正在使用Smack API开发一个聊天客户端,而notifyDatasetChanged()方法是在Smack的侦听器中调用的,而后者又在另一个线程中运行)。
正确的解决方案:notifyDatasetChanged应按如下方式调用:
lvFriendList.post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
如果有人遇到同样的问题,我希望这能为他们腾出时间:)。我也有同样的问题。您需要添加
@Override
public int getCount() {
return YOURLIST.length;
}
在你的适配器文件Thx中,你为我节省了几个小时的愤怒:)这是否意味着我也必须这样做@重写PostExecute上受保护的void(布尔值isSearchItemNotFound){mAgendaAdapterObj.notifyDataSetChanged();mAgendaRv.setAdapter(mAgendaAdapterObj);}
lvFriendList.post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
@Override
public int getCount() {
return YOURLIST.length;
}