保存动态创建的视图

Save dynamically created views

我正在研究 activity,用户可以在单击按钮时添加更多输入字段(2 个微调器和 1 个编辑文本)。在重新创建 activity 之前(旋转等),此机制工作正常。所有动态添加的视图都在 lost.I 寻找保存视图状态的最佳方式。不幸的是,我无法在 onSaveInstanceState() 方法中存储视图列表,也无法锁定屏幕方向。解决此问题的解决方案是什么? 这里是 activity java 代码

public class ReportActivity 扩展 AppCompatActivity {

private ImageButton addNew;
private LinearLayout mLayout;
private int categorySpinnerId =1001; //IDs of categorySpinner
private int currencySpinnerId =2001; //IDs of currencySpinner
private int editTextValueId =3001;   //Ids of editText


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report);
    Toolbar toolbar = (Toolbar) findViewById(R.id.report_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mLayout = (LinearLayout) findViewById(R.id.report_item_layout);
    mLayout.addView(createNewCategorySpinner());
    mLayout.addView(createNewEditText());
    mLayout.addView(createNewCurrencySpinner());

    addNew = (ImageButton) findViewById(R.id.btn_add_new);



    addNew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mLayout.addView(createNewCategorySpinner());
            mLayout.addView(createNewEditText());
            mLayout.addView(createNewCurrencySpinner());
        }
    });
}

//Generates operation category spinner
private Spinner createNewCurrencySpinner() {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final Spinner spinner = new Spinner(this);
    spinner.setLayoutParams(lparams);
    ArrayAdapter<CharSequence> currencySpinnerAdapter = ArrayAdapter.createFromResource(this,R.array.report_activity_currency_spinner,android.R.layout.simple_spinner_item);
    currencySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(currencySpinnerAdapter);
    spinner.setId(currencySpinnerId);
    spinner.setSaveEnabled(true);
    Log.d("ID", spinner.getId() + "");
    currencySpinnerId++;
    return spinner;
}

//Generates currency type spinner

private Spinner createNewCategorySpinner() {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final Spinner spinner = new Spinner(this);
    spinner.setLayoutParams(lparams);
    ArrayAdapter<CharSequence> categorySpinnerAdapter = ArrayAdapter.createFromResource(this,R.array.report_activity_category_spinner,android.R.layout.simple_spinner_item);
    categorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(categorySpinnerAdapter);
    spinner.setId(categorySpinnerId);
    spinner.setSaveEnabled(true);
    Log.d("ID", spinner.getId() + "");
    categorySpinnerId++;
    return spinner;
}

//Generates operation input value edit text

private EditText createNewEditText() {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final EditText editText = new EditText(this);
    editText.setLayoutParams(lparams);
    editText.setHint("Value");
    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
    editText.setId(editTextValueId);
    editText.setSaveEnabled(true);
    Log.d("ID", editText.getId() + "");
    editTextValueId++;
    return editText;
}

}

在您保存的实例状态 Bundle 中存储足够的信息,以便能够在新的 activity 实例中重新创建和重新填充动态创建的视图。