1

I have an array of custom objects called AccActivity.

public class AccActivity {
    AccData Data;
    AccData fData;
    AccData lpfData;

    int[] crossings;
    float[] sd;
    int type = -1;
    private float[] minMax;
    float spread = 0.30f;
    int rate = 8;
    float alpha = 0.30f;
    float avResAcc;
    float[] peakDistances;
    int[] peakNumber;
    ArrayList<Integer> peakIndicesX;
    ArrayList<Integer> peakIndicesY;
    ArrayList<Integer> peakIndicesZ;

And the AccData object looks like this:

public class AccData {
    private ArrayList<Float> xData;
    private ArrayList<Float> yData;
    private ArrayList<Float> zData;
    private float[] averageNoise;

Now, I have researched the problem on the internet and apparently there are three options of storing data in android - all of them seem not to be suitable for me

SharedPrefs - no, because its for storage of primitive key value preferences

Serialisation - saving to file - not sure - I only saw examples where it was used on non-custom object types, how would I come around it?

SQLite - no, another answer on stack overflow said: "How can I store my custom object in a database ?

You don't, unless you are using an object database. You store a representation of the object in a database."

What is this object database that they mention?

Can somebody tell me how to save and retrieve this arrayList that I'm having?

EDIT: I don't care about SDK compatibility and can select the highest one as this is an individual research project

EDIT2: Goal: After turning the phone off and back on, I want the ArrayList data to be automatically retrieved when I open the app.

LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65

3 Answers3

1

You can very well use Serialization. In case of custom objects, ensure that your custom object implements the Serializable interface, and any custom object within that object, should also be Serializable.

E.g:-

class CustomObject implements Serializable
{
     public String name;
}

ArrayList<CustomObject> objectList = new ArrayList<CustomObject>();

So objectList is now Serializable and you can also pass this between activities in the Intent.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • I tried using method from this link: http://stackoverflow.com/questions/11640122/save-an-arraylist-to-file-on-android but for some reason it did not work. – LucasSeveryn Mar 14 '13 at 06:38
  • I don't want to pass between activities, I want the data to be there after I turn the phone off and back on – LucasSeveryn Mar 14 '13 at 06:40
  • `some reason` is a bit too vague. If you've the whole error stack trace, can try to help you out with it. – Rahul Mar 14 '13 at 06:40
  • You can **also** pass, whether you want to pass or not, is upto you. But it'll be persisted in the device and you can read it back later. – Rahul Mar 14 '13 at 06:41
  • I have put this: `String ser = SerializeObject.ReadSettings(this, "myobject.dat"); if (ser != null && !ser.equalsIgnoreCase("")) { Object obj = SerializeObject.stringToObject(ser); // Then cast it to your object and if (obj instanceof ArrayList) { // Do something activityLibrary = (ArrayList)obj; } }` Into onCreate method and.... – LucasSeveryn Mar 14 '13 at 06:45
  • ... and this into the save method `String ser = SerializeObject.objectToString(activityLibrary); if (ser != null && !ser.equalsIgnoreCase("")) { SerializeObject.WriteSettings(this, ser, "myobject.dat"); } else { SerializeObject.WriteSettings(this, "", "myobject.dat"); }` The only thing I dont understand, is what is "act" variable - this is some context but it is not precised in answer – LucasSeveryn Mar 14 '13 at 06:46
1

You can serialize each array in AccData to JSON and store it in SQLite database.

delor
  • 800
  • 1
  • 7
  • 19
0

Answering your object database questions, there are at least 2 object databases which work on Android - db4o and Perst.

denis.solonenko
  • 11,645
  • 2
  • 28
  • 23