简体   繁体   English

在动态创建的UI中保存和还原实例状态?

[英]Saving and restoring instance state in dynamically created UI?

I'm creating an Android app with an activity comprised of a mixture of static views and dynamically created views. 我正在创建一个Android应用,其活动包含静态视图和动态创建的视图的混合。 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. 该活动包含保存在XML文件中的UI元素和根据模型类的内容动态创建的UI元素。 (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) (例如,在我的模型类中有一个数组,将为该数组中的每个元素创建一个EditText。.并且将创建一个Spinner,其中包含不同数组中的每个元素)

What's the usual method for saving and restoring the state of the application when dynamically created UI elements are involved? 涉及动态创建的UI元素时,保存和还原应用程序状态的常用方法是什么? Because I won't always know which UI elements will exist at any one time! 因为我永远不会知道任何时候都会存在哪些UI元素! Currently, my binding code simply reloads the data from the database when the device orientation changes, losing any changes that the user made. 当前,当设备方向改变时,我的绑定代码只是从数据库中重新加载数据,而丢失了用户所做的任何更改。

I've had a good look around on Google/SO for this and I've not found anything related to this problem. 为此,我在Google / SO上进行了很好的浏览,但没有发现任何与此问题相关的信息。

Thanks all 谢谢大家

Edit: For anyone that comes across this in the future, I did a slightly modified version of Yakiv's approach and wound up with this: 编辑:对于以后遇到此问题的任何人,我对Yakiv的方法进行了略微修改,并得出以下结论:

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.
        }
    }

Thanks again Yakiv for the awesome idea. 再次感谢Yakiv的出色建议。

To restore dynamic views state you need to make them as class fields and initialize them in onCreate . 要恢复动态视图状态,您需要将它们设置为class fields ,并在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++;
  }
}

Best wishes. 最好的祝愿。

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

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