在Android中存储用于检索的arraylist

时间:2012-10-29 23:56:01

标签: android eclipse arraylist

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

2 个答案:

答案 0 :(得分:2)

其中一个数据存储选项listed in the Android developer tutorial将是最简单的事情。哪种最适合取决于您存储的数据量以及访问方式。正如页面所说,SharedPreferences类最适合少数几个项目;对于较大的数据集,您可以在手机存储上使用Java序列化或其他方式write them to a file;如果您的数据很大和/或您需要对其进行结构化访问,an SQLite database是您最好的选择。

答案 1 :(得分:0)

您可以使用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);
        }
    }
}

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

仅供参考:使用SharedPreferences的一个含义是,如果用户卸载您的应用或清除应用数据,则会删除存储的列表。根据数据的性质,您可能会也可能不会想要这种行为。