1

I am having the worlds worst time trying to understand how to save anything perminately. All i want to do is save an Arraylist of ints that I can keep some high scores in. And I cant seem to find anyway to do it. I tried serializing the data and using file input stream to save it, but then there is no way to load it and desearlize the data because the only way to read the data from file input stream is to read it as an int. I tried writing and reading each number in the arraylist individually but then I get null pointer errors. Does anyone have a good way of doing this/have a good example of how to do this? Thanks in advance.

-Derek

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Derek
  • 151
  • 12
  • possible duplicate of [how can i serializable a ArrayList and Save in intern file](http://stackoverflow.com/questions/5513370/how-can-i-serializable-a-arraylist-and-save-in-intern-file) and http://stackoverflow.com/questions/5498039/how-can-i-save-an-arraylist-permanently-without-database and http://stackoverflow.com/questions/5513370/how-can-i-serializable-a-arraylist-and-save-in-intern-file and many more found by searching. – CommonsWare Nov 05 '11 at 22:25

3 Answers3

2

I would use an SQL-database instead of struggling with data files.

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

bos
  • 6,437
  • 3
  • 30
  • 46
1

Probably the simplest way is to serialize/deserialize your object from a String that you can store in a SharedPreference. The code below should do it for you:

public static void saveArray(Context ctx, int[] array) {
        String strArr = "";
        for (int i=0; i<array.length; i++) {
            strArr += array[i] + ",";
        }
        strArr = strArr.substring(0, strArr.length() -1); // get rid of last comma

        Editor e = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
        e.putString("MY_ARRAY", strArr);
        e.commit();
    }

    public static int[] getArray(Context ctx) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        String str = prefs.getString("MY_ARRAY", null);
        String[] strArr = str.split(",");
        int[] array = new int[strArr.length];
        for (int i=0; i<strArr.length; i++) {
            array[i] = new Integer(strArr[i]);
        }
        return array; 
    }
Damian
  • 8,062
  • 4
  • 42
  • 43
  • Alternatively you could just use `putStringSet(String key, Set values)` and `getStringSet(String key, Set defValues)`, although you'll still end up converting the int's to strings. – MH. Nov 05 '11 at 22:42
  • 1
    Also that is API 11 (Android v3.0.x) onwards only – Damian Nov 05 '11 at 22:51
0

I would make a class called PersistentManager or something add these two methods. You have to pass getApplicationContext() to these methods.

public static ArrayList<Integer> getArrayList(Context context)
{
    String line;
    ArrayList<Integer> list = new ArrayList<Integer>();
    StringBuilder fullText = new StringBuilder();

    try {
        FileInputStream stream = context.openFileInput("myFileName");
        InputStreamReader inputStreamReader = new InputStreamReader(stream); 
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while ((line = bufferedReader.readLine()) != null) 
        {
            fullText.append(line);
        }
        if (fullText.length() > 0) 
        {
            String[] myInts = fullText.toString().split(",");
            for (String intString : myInts)
            {
                list.add(Integer.getInteger(intString));
            }
        }
    } catch (Exception e) {}
    return list;
}

public static void setArrayList(ArrayList<Integer> myInts, Context context)
{
    FileOutputStream fos;
    try
    {
        fos = context.openFileOutput("myFileName", Context.MODE_PRIVATE);
        for (Integer integer : myInts)
        {
            fos.write((integer.toString() + ",").getBytes());
        }
        fos.close();
    } catch (FileNotFoundException e){} 
    catch (IOException e){}
}
Vinnie
  • 1,670
  • 14
  • 16