【发布时间】:2013-04-19 15:24:01
【问题描述】:
我正在使用 Fragments 创建一个联系人列表应用程序,其中一个片段是联系人列表中的姓名列表,另一个是其余详细信息。
这是显示名称列表的类
public class MyListFragment extends ListFragment {
private ContactStorage contactStorage = new ContactStorage();
public final static String TAG = "FRAGMENTS";
private MainActivity parent;
ArrayAdapter<String> adapter;
ArrayList<String> entries = new ArrayList<String>();
String array[];
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_layout, null);
parent = (MainActivity) getActivity();
entries = contactStorage.getContactListNames();
adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, entries);
setListAdapter(adapter);
Log.d(TAG, "Adapter created");
array = contactStorage.getContactDetails();
return v;
}
@Override
public void onResume() {
super.onResume();
entries = contactStorage.getContactListNames();
adapter.notifyDataSetChanged();
Log.d(TAG, "List Frag Resumed");
}
}
我遇到的问题是 ArrayAdapter 在恢复时没有刷新。
当屏幕旋转时,它可以再次运行 onCreateView(),但我需要它来刷新 onResume。我查看了这个网站的加载情况,除了“使用 notifyDataSetChanged()”之外什么也没发现。
【问题讨论】:
-
好吧,你期待什么?您正在检索
entries但 aret re-building youradapter. Move the two linesentires =` 和adapter =从onCreate()到onResume()(你应该只这样做一次),忘记entries =和notify在onResume(),你会没事的。 -
@ClassStacker - 还是同样的问题。在屏幕旋转之前不刷新列表。
-
嗯,是的,抱歉,您还必须在那里执行 setListAdapter()。
-
@ClassStacker - 我可以看到它是如何工作的,但这会更像是一种“hacky”方式吗?
-
我的建议和你接受的一样。它没有什么“hacky”。只是
clear()/addAll()的方法比直接扔掉整个en bloc要慢。
标签: android refresh fragment android-arrayadapter