在Android中存储用于检索的arraylist

在Android中存储用于检索的arraylist,android,eclipse,arraylist,Android,Eclipse,Arraylist,我目前正在开发一个Android应用程序,它有点像一个学生计划员。我的后端都是java,我现在被卡住了,因为我正在将我从后端创建的对象存储到arraylist。作为java,一旦程序终止,这些对象就会消失。存储java对象以便下次启动应用程序时检索的最简单方法是什么?非常感谢您的帮助!我是在2.3和eclipse(juno)上开发的。其中一个数据存储选项将是最容易做到的。哪种方式最适合将取决于您存储的数据量以及您需要如何访问数据。正如页面上所说,这门课最适合一些项目;对于更大的数据集,您可以使用

我目前正在开发一个Android应用程序,它有点像一个学生计划员。我的后端都是java,我现在被卡住了,因为我正在将我从后端创建的对象存储到arraylist。作为java,一旦程序终止,这些对象就会消失。存储java对象以便下次启动应用程序时检索的最简单方法是什么?非常感谢您的帮助!我是在2.3和eclipse(juno)上开发的。

其中一个数据存储选项将是最容易做到的。哪种方式最适合将取决于您存储的数据量以及您需要如何访问数据。正如页面上所说,这门课最适合一些项目;对于更大的数据集,您可以使用Java序列化或其他方式来存储手机上的数据;如果您的数据很大,并且/或者您需要对其进行结构化访问,这是您的最佳选择。

您可以使用存储数据。
SharedReferences
允许您在应用程序中保留基本数据类型的键值对。不能用单个键存储整个
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);
        }
    }
}
公共类SomeActivity扩展活动{
private ArrayList list;//我们将持久化此数组
/*剪断*/
//这些字符串可以是任何东西-您只需要一些可以用来系统地生成不同键的东西
公共静态最终字符串列表\u KEY=“SomeActivity\u LIST”;
公共静态最终字符串列表\u LENGTH\u KEY=“SomeActivity\u ListLength”;
/**
*此方法的工作原理:首先获取SharedReferences对象,
*它提供了一个用于持久化数据的API,然后系统地生成
*字符串,如“SomeActivity_List1”、“SomeActivity_List2”、“SomeActivity_List3”,
*依此类推,用作列表中第一、第二、第三等元素的键。然后
*它是Data.saveData(),下面在Data类中定义的一种方法,用于
*ArrayList中的每个数据对象都有机会保留其原语
*SharedReferences中的成员。
*
*SomeActivity.restoreList()的工作原理类似。
*/
公共void存储列表(){
SharedReferences prefs=getPreferences();//此方法是Activity类的一部分
SharedReferences.Editor=prefs.getEditor();
//保存列表的长度,以便在恢复列表时知道有多少个
//要重新创建的数据对象。
editor.putInt(LIST_LENGTH_KEY,LIST.size());
commit();
//这个for循环很重要——请注意,我们如何将i连接到每个键,从而为列表中的每个元素提供一个不同的键
对于(int i=0;i
这种方法是递归的。例如,如果数据有一个ArrayList作为其字段之一,那么它也可以系统地将该列表中的所有值存储在SharedReferences中

仅供参考:使用SharedReference的一个含义是,如果用户卸载您的应用程序或清除应用程序数据,存储的列表将被删除。根据数据的性质,您可能希望也可能不希望出现这种行为