简体   繁体   English

Android:在选项卡式应用程序中以片段形式保留listview内容的最佳方法?

[英]Android: Best way to retain listview content in fragment in tabbed app?

Firstly, this is my first question on StackOverflow so please forgive any formatting issues etc. 首先,这是我关于StackOverflow的第一个问题,因此请原谅任何格式问题。

I'm writing an Android tablet application which allows users to populate a ListView with content pulled from a web service. 我正在编写一个Android平板电脑应用程序,该应用程序允许用户使用从Web服务提取的内容填充ListView。 At the minute there is one activity which contains a ViewPager which contains 6 fragments on different pages. 在此刻,有一个活动包含一个ViewPager,该ViewPager在不同页面上包含6个片段。

The issue is that the data which is downloaded and added to the ListView disappears when the user changes to another tab/back again and the Fragment's OnCreateView method is called (which initialises the ListView back to empty) 问题是,当用户再次切换到另一个选项卡/上一步,并且调用了Fragment的OnCreateView方法(将ListView初始化为空)时,下载并添加到ListView的数据消失了。

What's the best way of keeping the content of the listview besides storing it in a database/sharedprefs. 除了将列表视图的内容存储在数据库/ sharedprefs中之外,什么是保留列表视图的内容的最佳方法是什么。 The data in the listview needs to be destroyed when the user logs out/restarts the application, so I'm hoping to not persist it in any way. 当用户注销/重新启动应用程序时,列表视图中的数据需要销毁,因此我希望不要以任何方式保留它。

the OnCreateView of the ListView container fragment: ListView容器片段的OnCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_certificate_list, container, false);
    return rootView;
}

And here's the code which populates the ListView when the user hits the download button: 这是当用户点击下载按钮时填充ListView的代码:

ListView lv = (ListView) callerActivity.findViewById(R.id.listView1);
lv.setAdapter(new CertListAdapter(callerActivity.getBaseContext(), activeCerts));
if (activeCerts.size() > 0) {
       ((TextView) callerActivity.findViewById(R.id.certStatusLabel)).setVisibility(View.GONE);
} else {
        ((TextView) callerActivity.findViewById(R.id.certStatusLabel)).setText(callerActivity.getString(R.string.certsDontExist));
}

the activeCerts variable is an arraylist of certificate objects which are obtained from the web service. activeCerts变量是从Web服务获得的证书对象的数组列表。

EDIT 编辑

Found a solution for this, to simply check if the "activeCerts" variable is null in the onCreateView method and populate the ListView again if it's not null. 找到了解决方案,只需检查onCreateView方法中“ activeCerts”变量是否为null,如果不为null则再次填充ListView。 When the user logs out I'll just set activeCerts to null. 当用户注销时,我只是将activeCerts设置为null。

Cheers 干杯

"Correct" way: “正确”的方式:

use Fragment.onSaveInstanceState(Bundle) and Fragment.onActivityCreated(Bundle) to save and restore your items 使用Fragment.onSaveInstanceState(Bundle)Fragment.onActivityCreated(Bundle)保存和还原项目

Cheap way: 便宜的方式:

set android:configChanges="orientation|keyboardHidden|screenSize" on your activity hosting the fragment 在托管片段的活动上设置android:configChanges="orientation|keyboardHidden|screenSize"

You can also setRetainInstance(true); 您也可以setRetainInstance(true); in the onCreate() of your Fragment 在片段的onCreate()

Read more about all this here . 在这里阅读更多关于这一切的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM