8

I have a question about saving an arraylist of custom objects. I have a class called notitie:

public class Notitie implements Serializable{

private String titel = "";
private String type = "";
private String datum = "";

public void setTitel (String titel){
    this.titel = titel;
}
public String getTitel(){
    return titel;
}
public void setType (String type){
    this.type = type;
}
public String getType(){
    return type;
}
public void setDatum (String datum){
    this.datum = datum;
}
public String getDatum(){
    return datum;
}
}

I create some objects of Notitie and add them to my arraylist called notities

ArrayList<Notitie> notities = new ArrayList<Notitie>();

Notitie notitie1 = new Notitie();
notitie1.setTitel("Meting");
notitie1.setType("Watermeting");
notitie1.setDatum("22-09-12");
notities.add(notitie1);

Notitie notitie2 = new Notitie();
notitie1.setTitel("Meting2");
notitie1.setType("Watermeting2");
notitie1.setDatum("23-09-12");
notities.add(notitie2);

Notitie notitie3 = new Notitie();
notitie1.setTitel("Meting3");
notitie1.setType("Watermeting3");
notitie1.setDatum("24-09-12");
notities.add(notitie3);

Now I want to save the filled Arraylist on the device's storage so it can be accessed anytime. I used to save data as a String or some Integers with sharedpreferences but I can't save this Arraylist with that. Does anybody have a solution?

Thanks in advance!

Simon
  • 2,328
  • 6
  • 26
  • 30

1 Answers1

6

You do have a few options:

  1. Use serialization, XML or JSON, and store your data in a File. Refer to this solution if you wanna implement serialization.

  2. Store you data using SQLite. Have a look at this tutorial to get started.

EDIT : You might want to read this as well!

Community
  • 1
  • 1
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • Why or in which case would you recommend the first or the second approach? – Bruiser Oct 14 '13 at 16:54
  • 3
    @Bruiser : That is a very good question. The advantage of SQLite is that you can directly display the elements in any ListView using the CursorAdapter, which saves tons of work. Plus, handling Cursor data is really easy once you get the hang of it. Doing the same things using Files might get a little painstaking, if not frustrating. So, for me, SQLite is the way to go. – Swayam Oct 14 '13 at 23:26
  • 1
    Thanks a lot for the detailed explanation. I understand that there is of course a lot to consider but that is definitely a very good starting point for further discussion. Here is some more for those who are interested: http://stackoverflow.com/questions/12212640/android-performance-flat-file-vs-sqlite | http://stackoverflow.com/questions/1803365/android-data-storage-file-vs-sqlite – Bruiser Oct 15 '13 at 09:42
  • Couldn't agree with you anymore. And yes, there is a lot to consider, but for me, SQLite usually does the job. Anyway, good luck ahead. Cheers. – Swayam Oct 15 '13 at 13:30