【发布时间】:2014-02-24 15:39:32
【问题描述】:
我有一个类MyArrayAdapter,它扩展了ArrayAdapter<MyClass>。
现在我有一个MyListFragment,它扩展了ListFragment,它使用了MyArrayAdapter。
MyActivity 类在视图中添加 MyListFragment。
到目前为止一切顺利。
现在,用户可以更改首选项,在此基础上我需要更改List<MyClass> 中的一些字符串。由于 ArrayAdapter 仅在 clear(), add(), etc 等自己的方法用于 List<MyClass> 时才识别 onNotifyDataSetChanged(),而我没有这样做,因此我使用 onResume 重新加载数据以反映更改。
所以我的MyListFragment 包含以下内容:
public class MyListFragment extends ListFragment {
private List<MyClass> elements = null;
private MyListAdapter myListAdapter = null;
public MyListFragment(List<MyClass> elements) {
this.elements = elements;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myListAdapter = new MyListAdapter(inflater.getContext(), R.layout.foo, elements);
setListAdapter(myListAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onResume() {
myListAdapter.clear();
myListAdapter.addAll( sqliteclass.getAllElements() );
myListAdapter.notifyDataSetChanged();
isSecondTime = true;
super.onResume();
}
}
这可行,但问题是当活动第一次运行时,现在有 2 次行程 到需要一次的数据库。所以我修改了类如下:
public class MyListFragment extends ListFragment {
private List<MyClass> elements = null;
private MyListAdapter myListAdapter = null;
private boolean isSecondTime = false; //NEW
public MyListFragment(List<MyClass> elements) {
this.elements = elements;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myListAdapter = new MyListAdapter(inflater.getContext(), R.layout.foo, elements);
setListAdapter(myListAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onResume() {
if(isSecondTime){ //NEW
myListAdapter.clear();
myListAdapter.addAll( sqliteclass.getAllElements() );
} //NEW
myListAdapter.notifyDataSetChanged();
isSecondTime = true;
super.onResume();
}
}
所以我的问题是,我通过使用 boolean isSecondTime 来确保我第一次不进行 2 次访问数据库的操作是否可靠?适配器保存了元素列表,所以第二次以后我只做了一次。
(例如,如果用户更改了首选项,切换到其他应用程序,Android 决定释放一些内存,并从内存中删除 MyListFragment,当用户切换回来时,我可以确保List<Elements> 会从数据库中恢复过来吗?- 这个或任何类似的场景)
欢迎提出任何建议。
【问题讨论】:
-
如果您的数据来自(至少 sqliteclass 的名称如此暗示)sqlite,您为什么要使用 ArrayAdapter?使用 SimpleCursorAdapter
-
听起来不错。我会看看
SimpleCursorAdapter是否更适合我的需要并回来.. -
考虑研究 SimpleCursorAdapter
标签: android android-fragments android-arrayadapter