Jus*_*r32 3 android listview save android-listview
我正在创建待办事项列表.现在我的应用程序在ListView中添加和删除数据.但是,当我关闭我的应用程序时,数据将被删除.我想使用SharedPreferences来保存数据onDestroy(),然后当我的应用程序打开时,我想要重新加载数据.
但是,我不知道如何完成这项任务. 任何人都可以帮助我使用共享首选项(也就是我正在寻找代码)保存ListView?
有一个共享首选项的教程我希望在我的应用程序关闭时动态添加多个字符串.然后,当我重新打开它时,数据将出现.我只使用ONE ACTIVITY页面,一切都会在一页上发生.
这是我的代码:
public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
// set ListView item listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Confirm Delete");
alertDialogBuilder.setMessage("Sure you want to delete?");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
adapter.remove(adapter.getItem(position));
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
adapter.add(input);
et.setText("");
}
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main__to_do_list, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
正如Stefano Munarini所建议的那样,您可以使用数据库.
另一方面,您可以循环访问ArrayAdapter的元素(包装ArrayList)并将它们存储到SharedPreferences中,如下所示:
public static final String PREFERENCES_TODO = "TODO_List_Shared_Preferences";
// ...
SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_TODO,
MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// This will remove all entries of the specific SharedPreferences
editor.clear();
for (int i = 0; i < adapter.getCount(); ++i){
// This assumes you only have the list items in the SharedPreferences.
editor.putString(String.valueOf(i), adapter.getItem(i));
}
editor.commit();
// ...
// And the reverse process:
SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_TODO,
MODE_PRIVATE);
for (int i = 0;; ++i){
final String str = prefs.getString(Integer.valueOf(i), "");
if (!str.equals("")){
adapter.add(str);
} else {
break; // Empty String means the default value was returned.
}
}
Run Code Online (Sandbox Code Playgroud)
一些考虑:
SharedPreferences.Editor.putStringSet(String, Set<String>)来使用单个条目.| 归档时间: |
|
| 查看次数: |
20866 次 |
| 最近记录: |