保存动态创建的视图

时间:2015-12-14 23:32:51

标签: android android-view savestate

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

公共类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;
}

}

1 个答案:

答案 0 :(得分:1)

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