【发布时间】:2015-12-14 23:32:51
【问题描述】:
我正在开发用户可以在单击按钮时添加更多输入字段(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;
}
}
【问题讨论】:
标签: android android-view savestate