在动态创建的UI中保存和恢复实例状态? [英] Saving and restoring instance state in dynamically created UI?

查看:116
本文介绍了在动态创建的UI中保存和恢复实例状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含静态视图和动态创建的视图混合的活动的Android应用程序。该活动包含保存在XML文件中的UI元素和基于模型类的内容动态创建的UI元素。 (如,在我的模型类中有一个数组,并且将为数组中的每个元素创建一个EditText),并创建一个Spinner,其中包含不同数组中的每个元素)

I'm creating an Android app with an activity comprised of a mixture of static views and dynamically created views. The activity contains UI elements held in an XML file and UI elements which are created dynamically based on the content of a model class. (as in, there is an array in my model class and an EditText will be created for every element in the array.. and a Spinner will be created containing every element in a different array)

当涉及到动态创建的UI元素时,常用的保存和恢复应用程序状态的方法是什么?因为我不会总是知道任何一个UI元素将存在!目前,我的绑定代码只是在设备方向更改时重新加载数据库中的数据,从而丢失了用户所做的任何更改。

What's the usual method for saving and restoring the state of the application when dynamically created UI elements are involved? Because I won't always know which UI elements will exist at any one time! Currently, my binding code simply reloads the data from the database when the device orientation changes, losing any changes that the user made.

我已经很好看了Google / SO为此,我没有发现任何与此问题有关的问题。

I've had a good look around on Google/SO for this and I've not found anything related to this problem.

感谢所有

编辑:
对于将来遇到这个问题的任何人,我做了一个稍微修改版本的Yakiv的方法,并清理了:

For anyone that comes across this in the future, I did a slightly modified version of Yakiv's approach and wound up with this:

for (int i = 0; i < views.size(); i++) {
        View currentView = views.get(i);
        if (currentView instanceof CheckBox) {
            outState.putBoolean("VIEW"+i, (((CheckBox) currentView).isChecked()));
        } else if (currentView instanceof EditText) {
            outState.putString("VIEW"+i, ((EditText) currentView).getText().toString());
        } else if (currentView instanceof Spinner) {
            //.....etc. etc.
        }
    }

再次感谢Yakiv的真棒想法。 / p>

Thanks again Yakiv for the awesome idea.

推荐答案

要恢复动态视图状态,您需要将其作为类字段并在 onCreate 中初始化它们。然后只需使用文章即可保存并恢复其状态。

To restore dynamic views state you need to make them as class fields and initialize them in onCreate. Then just use this article to save and restore their state.

private List<View> mViews= new ArrayList<View>();

@Override
public void onCreate(..)
{
  LinearLayout parent = ....//initialize it here
  initializeViews();
}

public void initializeViews()
{
  // create and add 10 (or what ever you need) views to the list here
  mViews.add(new View(this));
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  int mViewsCount = 0;
  for(View view : mViews)
  {
     savedInstanceState.putInt("mViewId_" + mViewsCount, view.getId());
     mViewsCount++;
  }

  savedInstanceState.putInt("mViewsCount", mViewsCount);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  int mViewsCount = savedInstanceState.getInt("mViewsCount");;
  for(i = 0; i <= mViewsCount)
  {
     View view = mViews.get(i);

     int viewId = savedInstanceState.getInt("mViewId_" + i);
     view.setId(viewId);

     mViewsCount++;
  }
}

祝愿。

这篇关于在动态创建的UI中保存和恢复实例状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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