-1
我正在处理用户可以在按钮单击时添加更多输入字段(2个旋钮和1个编辑文本)的活动。这个机制工作正常,直到重新创建活动(循环等)。所有动态添加的视图都会丢失。我正在寻找最佳方式来保存视图状态。不幸的是,我不能在onSaveInstanceState()方法中存储视图列表,我也无法锁定屏幕方向。什么可以解决这个问题? 下面是活动的Java代码保存动态创建的视图
公共类ReportActivity扩展AppCompatActivity在你保存的实例状态Bundle
{
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;
}
}