Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the method for storing arrays or custom objects (persistent data)?

Is there any way to store a custom data object as persistent data without employing SQLite in Android?

I have a 3*3 matrix of EditText fields on screen, and I want to store all the content of those 9 fields into one "profile" (object). Other profiles can be created to have a different matrix with different data.

I thought of XML, but I would like to hear other opinions.

like image 353
Armando Avatar asked Jun 10 '11 20:06

Armando


2 Answers

You could serialize the array and store it to the shared preferences, similar to the answer to this question:

Android Sharepreferences and array

To store multiple profiles you could possibly store each in the shared preferences with a key like "Profile-1", "Profile-2", etc. You could then have another shared preference that has the number of Profiles you have.

like image 108
John Boker Avatar answered Nov 15 '22 00:11

John Boker


You could try something like

public bool save(SaveableObject s, Context context, String fileName)
{
    try
    {
        FileOutputStream fos;
        ObjectOutputStream os;
        File file1 = context.getExternalFilesDir(null);
        File file2 = new File(file1, fileName);
        fileName = file2.getAbsolutePath();
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        os = new ObjectOutputStream(fos);
        os.writeObject((Object)analysis);
        os.close();
        fos.close();
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

which should save a file to /sdcard/fileName.

like image 42
fortyCakes Avatar answered Nov 14 '22 23:11

fortyCakes