ArrayList<String> checkedValue2;
我有一个arrayList,其中我添加了检查的值,工作正常。检查/未检查工作正常,但问题是当应用程序重新启动时,arrayList为empty.how以保存arraylist对象.again当app重新启动时我检查取消选中它值添加删除。 这是我的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkedValue = new ArrayList<String>();
当我点击复选框时,其值加载到arraylist,当我取消选中该值时删除app重启时arraylist为空plz告诉我如何保存arraylist aftar app restart.help我的一些代码
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
cb = (CheckBox) v.findViewById(R.id.checkBox1);
TextView tv = (TextView) v.findViewById(R.id.textView);
pi = (ApplicationInfo) arg0.getItemAtPosition(position);
cb.performClick();
if (cb.isChecked()) {
checkedValue.add(tv.getText().toString());
itemCheckedd[position] = true;
// Toast.makeText(MainActivity.this, "all" + position, Toast.LENGTH_LONG).show();
} else if (!cb.isChecked()) {
checkedValue.remove(tv.getText().toString());
}
}
答案 0 :(得分:0)
嗨,请阅读此链接 1&gt;将数据存储在saveInstanceState
中 static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
2';检索数据
@Override
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //始终先调用超类
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
您将把数据存储在savedInstanceState中,并在活动重启后从savedInstanceState
中获取数据