保存一个Android的ListView先进国家 [英] Saving advanced state of an Android Listview

查看:98
本文介绍了保存一个Android的ListView先进国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,Android提供了一种机制,允许开发人员通过以下方法在活动保存和恢复状态:

I know that Android provides a mechanism to allow developers to save and restore states in an Activity via the following methods:

protected void onSaveInstanceState (Bundle outState)
protected void onRestoreInstanceState (Bundle savedInstanceState)

我见过无数的例子即人民展示如何保存简单的状态(如在ListView当前索引),以及许多例子,人们说,列表视图应该只是一个数据模型,这将使​​该列表备份得到恢复。然而,当事情变得更加复杂和用户交互更多地参与,我一直在努力找出正确的方式来保存这个状态。

I've seen numerous examples whereby people show how to save simple state (such as the current index in a listview), and many examples where people say that the listview should just be backed by a data model which will allow the list to be restored. However, when things get more complicated and the user interaction more involved, I've been struggling to find out the correct way to save this state.

我有一个包含自定义控件的每一行列表视图。当单击某一行将以动画展开以显示更多详细信息给用户...内的其他详细信息视图中,用户还能够改变某些事情。现在我知道,如果我的活动被销毁(或我的片段代替),目前的状态反映了用户界面的变化和用户的互动名单现在失去了。

I have a listview which contains custom controls for each row. When clicked a row will expand with animation to show additional details to the user... within the additional details view, the user is also able to change certain things. Now I know that if my Activity is destroyed (or my Fragment replaced), the current state reflecting the UI changes and the user's interaction with the list is now lost.

我的问题是我怎么有大约节省了正确的ListView我这种精细状态,使得每个自定义组件行的状态也preserved?

My question is how do I got about correctly saving this detailed state of my listview such that the state of each custom component row is also preserved?

谢谢!

编辑:感谢下面的答复。据我所知,共享preferences可以用来存储键 - 值状态,不过,我想我更感兴趣的是视图状态....例如Android将自动保存/恢复一个EditText字段的视图状态(即它的内容)在我的活动,让我怎么去在ListView更复杂的自定义组件实现同样的事情?难道我真的要全部手动保存这个状态我自己(即节省到底是哪的ListView行已扩大,保存所有的用户界面的变化用户做出/切换等)?

Thanks for the replies below. I understand that SharedPreferences can be used to store key-value state, however, I guess I am more interested in the view states.... for instance Android will automatically save/restore the viewstate of an EditText field (i.e. its contents) in my activity , so how do I go about achieving the same thing with more complex custom components in a listview? Do I really have to save this state all manually myself (i.e. save exactly which listview rows have been expanded, save all the ui changes the user has made/toggled, etc.)?

推荐答案

我不喜欢这样说,但似乎这都必须做你自己。该框架不提供pre-制作方法为保存视图状态。

I don't like to say this, but it seems that this must all be done yourself. The framework does not offer a pre-made method for "save state of view".

无论您保存在一个包或者国家共享preferences,我认为这一点是需要确定的状态变量,反映在当前列表中选择,自己坚持他们, onResume()你需要的状态重新引入到您的活动。

Whether you save the state in a Bundle or SharedPreferences, I think the point is that you need to determine state variables that reflect your current list choices, persist them yourself, and onResume() you need to reintroduce that state into your activity.

...相当手工工艺,像你说的......

... quite a manual process, as you say ...

这篇关于保存一个Android的ListView先进国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆