如何保存 ListView 并通过简历恢复它?因为当我关闭应用程序时,所有内容都会被删除。
感谢您的回答
公共类 ToDoList 扩展 Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView)findViewById(R.id.todolist);
final EditText myEditText = (EditText)findViewById(R.id.addtodo);
final ArrayList todoItems = new ArrayList();
final ArrayAdapter aa;
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1,
todoItems);
myListView.setAdapter(aa);
String edittext="";
SharedPreferences settings = getSharedPreferences("PreferencesName", 0);
edittext = settings.getString("Content", edittext);
todoItems.add(0, edittext);
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_ENTER){
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
String edittext= myEditText.getText().toString();
SharedPreferences settings = getSharedPreferences("PreferencesName", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("Content", edittext);
myEditText.setText("");
return true;
}
return false;
}
});
}}