0

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

4

2 回答 2

2

Android 开发人员教程中列出的数据存储选项之一将是最简单的操作。哪个最合适取决于您存储的数据量以及您需要如何访问它。正如页面所说,SharedPreferences类最适合少数项目;对于较大的数据集,您可以使用 Java 序列化或其他方式将它们写入手机存储中的文件;如果您的数据很大并且/或者您需要对它们进行结构化访问,那么 SQLite 数据库是您的最佳选择。

于 2012-10-30T00:06:37.800 回答
0

您可以使用共享首选项来存储您的数据。允许您在应用程序中保留原始数据类型的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);
        }
    }
}

这种方法递归地工作。例如,如果 Data 有一个 ArrayList 作为其字段之一,它也可以系统地将该列表中的所有值存储在 SharedPreferences 中。

仅供参考:使用 SharedPreferences 的影响之一是,如果用户卸载您的应用程序或清除应用程序数据,则存储的列表将被删除。根据数据的性质,您可能需要也可能不想要这种行为。

于 2012-10-30T00:32:45.350 回答