ListView savedInstanceState运行不正常

时间:2016-06-01 18:46:36

标签: java android listview android-studio arraylist

我有一个ListView,当我添加项目时,我用以下内容保存:

static ArrayList<String> notes = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putStringArrayList("notes", notes);
}

在onCreate中我得到了它:

if (savedInstanceState == null) {
    notes.add("Test");
} else {
    notes = savedInstanceState.getStringArrayList("notes");
}

arrayAdapter = new ArrayAdapter<>(this, R.layout.item, notes);
listView().setAdapter(arrayAdapter);

笔记正在保存,除了savedInstanceState为空之外,一切都还可以,当我再次启动应用程序时,每次都会添加“测试”,我的错误在哪里?

0 个答案:

没有答案