0

I've tried to get my head around this, but can't so asking the forum. Does anyone think it would be possible to save multiple ArrayLists to a file? Would I have to create multiple files for each List or could I bung them all in one with a reference of some sort? So far I have knocked up a quick experiment but to no avail. (this is just a quick representation of my code but I am away from the computer)

try {
    fos = new FileOutputStream(file);
    oos = new ObjectOutputStream(fos);

    oos.writeObject(Ing);
    oos.writeObject(IngN);
    oos.writeObject(Step);

    oos.close();
    fos.close();
}

etc

1 Answer 1

1

You would be best to serialize the arraylists to something like json first, and then write that to a file.

You could create a simple class that holds 2 arraylists, and then serialize that class so you end up with one string that contains both arraylists, which you can write to one file.

For serializing to json, check out jackson mapper.

I'm not sure what your use case is, but you may want to also look into saving the data into a database instead of a file.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply, I didn't want to get into databases and Json as I'm fairly new to this as it is. But I guess it's time to start learning more. I want to store three values, consisting of ingredient quantities and names which can be as a pair, and also recipe steps which can be on their own. I want to be able to call back these values and then programmatically set up TextViews in a LinearLayout. Do you know of any tutorials or online help which may be of assistance?
I would recommend taking a look at activeandroid.com. It's very simple especially for a beginner. For storing recipes, you could get quite complex and have multiple tables etc, but for starting off, I recommend just using one table. Store your ingredients pair as a string. So write a small function that takes the pair and converts it to a comma separated string. That way your recipe object just has 2 string fields - ingredients, and steps, and it will be really easy to store that in a database with active android. You will need to convert the ingredient string back into a pair.
Thanks! That sounds very do-able, I'm going to look at Json first but I'll definitely give Active Android a go! Cheers
Sorry for the very nooby question but I haven't needed to do this before. How do I add the add-on to eclipse?, Looked everywhere but I don't know the correct terminology. Is it an add-on? or existing project or what? User library?
You should clone the library from Github (github.com/pardom/ActiveAndroid), use the git clone command. Then import that library into eclipse with the File -> Import -> Existing Android Code. Make sure the project is marked as a library in the Properties -> Android tab. Then add that library as a dependency to your project in the same place (but looking at your project properties instead).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.