我目前正在开发一个Android应用程序,它将有点像学生计划器。我的后端都是java的,我现在被卡住了,因为我把我从后端创建的对象存储到arraylist中。作为java,一旦程序终止,这些对象就消失了。什么是最简单的方法,我可以存储我的java对象,以便在下次启动我的应用程序时检索?任何帮助都是非常感谢的!我正在2.3和eclipse(juno)上开发。
发布于 2012-10-30 08:06:38
作为数据存储选项之一,listed in the Android developer tutorial将是最容易做的事情。哪个是最合适的将取决于您存储了多少数据以及您需要如何访问这些数据。正如页面所说,SharedPreferences类最适合少数几个项目;对于较大的数据集,您可以使用Java或其他方法在电话存储上进行write them to a file;如果您的数据很大,并且/或者您需要对它们进行结构化访问,那么an SQLite database是最好的选择。
发布于 2012-10-30 08:32:45
您可以使用Shared Preferences来存储数据。SharedPreferences允许您在应用程序中持久化原始数据类型的键值对。不能用单个键存储整个ArrayList,但是可以迭代数组并系统地为列表中的每个值生成一个键。我通常这样做:
public class SomeActivity extends Activity {
private ArrayList<Data> list; //We'll persist this array
/* snip */
//These strings can be anything - you just need something you can use to systematically generate distinct keys
public static final String LIST_KEY = "SomeActivity_List";
public static final String LIST_LENGTH_KEY = "SomeActivity_ListLength";
/**
* How this method works: It starts by getting a SharedPreferences object,
* which offers an API for persisting data. It then systematically generates
* Strings like "SomeActivity_List1", "SomeActivity_List2", "SomeActivity_List3",
* and so on to use as keys fot the 1st, 2nd, 3rd, etc. elements in the list. Then
* it Data.saveData(), a method defined below in the Data class, in order to give
* each Data object in the ArrayList an opportunity to persist its primitive
* members in the SharedPreferences.
*
* SomeActivity.restoreList() works similarly.
*/
public void saveList() {
SharedPreferences prefs = getPreferences(); //This method is part of the Activity class
SharedPreferences.Editor editor = prefs.getEditor();
//Save the length of the list so that when we restore it, we know how many
//Data objects to recreate.
editor.putInt(LIST_LENGTH_KEY, list.size());
editor.commit();
//This for loop is important - note how we concatenate i to each of the keys to give each element in list a distinct key
for(int i = 0; i < list.size(); i++)
{
String identifier = LIST_KEY + Integer.toString(i); //generate distinct keys
list.get(i).saveData(identifier, prefs);
}
}
public void restoreList() {
SharedPreferences prefs = getPreferences();
int length = prefs.getInt(LIST_LENGTH_KEY);
for(int i = 0; i < length; i++)
{
String identifier = LIST_KEY + Integer.toString(i); //re-generate distinct keys
Data data = new Data();
data.restoreData(identifier, prefs);
list.addLast(data);
}
}
public static class Data {
private int i;
private double j;
private String s;
public static final String I_KEY = "Data_I"
public static final String J_KEY = "Data_J" //strings can really be whatever, as long as they're distinct.
public static final String S_KEY = "Data_K"
/**
* How this method works: The SomeActivity.saveList() method generates a
* unique String ("identifier") for each of the Data objects it contains.
* This method uses that distinct string and makes some more distinct keys
* to store each of Data's primitive members.
*
* restoreData() works similarly when it rebuilds Data objects
*/
public saveData(String identifier, SharedPreferences prefs) {
SharedPreferences.Editor editor = prefs.getEditor();
editor.putInt(I_KEY + identifier, i);
editor.putDouble(J_KEY + identifier, j);
editor.putString(S_KEY + identifier, s);
editor.commit();
}
public restoreData(String identifier, SharedPreferences prefs) {
i = prefs.getInt(I_KEY + identifier);
j = prefs.getDouble(J_KEY + identifier);
s = prefs.getString(S_KEY + identifier);
}
}
}这种方法以递归方式工作。例如,如果数据的其中一个字段是ArrayList,那么它也可以在SharedPreferences中系统地存储该列表中的所有值。
仅供参考:使用SharedPreferences的一个含义是,如果用户卸载您的应用程序或清除应用程序数据,存储的列表将被删除。根据数据的性质,您可能想要也可能不想要此行为。
https://stackoverflow.com/questions/13131360
复制相似问题