Android的onResume更新列表适配器适配器、列表、Android、onResume

2023-09-12 22:20:40 作者:碧海蓝天白婚纱

我用一个列表适配器来显示不同​​的商店,当有人选择一个存储需要他们到一个新的活动,在那里他们可以添加商店收藏的屏幕上。

I'm using a list adapter to show different stores, when someone selects a store it takes them to a new activity where they can add the store to favorite on that screen.

有一个后退按钮调用结束(); 可以追溯到与ListView屏幕。

There is a Back button on that calls finish(); that goes back to the screen with the listview.

现在的问题是,不更新的列表视图(即不显示,这家店被添加到最爱的话)。我想这code,但没有运气:

Now the problem is the listview isn't updated (ie. doesn't show that the store is added to favorite already). I tried this code but no luck:

@Override
public void onResume() {
    super.onResume();
    list.setAdapter(null);      
    updateMyList();
    adapter=new LazyAdapter(this, ((String[])names.toArray(new String[0])), 
        ((String[])status.toArray(new String[0])));
    list.setAdapter(adapter);
}

updateMyList()调用服务器API和更新的名称和状态数组。

updateMyList() calls the server API and updates the names and status arrays.

通过这个$ C C列表$并没有真正更新...

With this code the list doesn't really update...

推荐答案

您应该setAdapter在你的的onCreate()只,在 onResume() 你刚刚打电话 adapter.notifyDataSetChanged()数据的新的集合。这将刷新您的ListView数据的新的集合。

You should setAdapter in your onCreate() only, inside onResume() you just had to call adapter.notifyDataSetChanged() with the new collection of data. This will refresh your ListView with the new collection of data.