-1

as the title suggests I am having trouble saving a pojo onto my phones internal storage through SharedPreferences. Here is my code to create the pojo:

class dataPOJO{
String fileName;
String cName;
String[] cN;


public String getName() {
    return cName;
}

public void setName(String cName) {
    this.cName = cName;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String[] getText() {
    return cN;
}

public void setExtras(String[] cN) {
    this.cN = cN;
}

public String toString() {
    String savePackage = "File Name: " + getFileName() + "\n";
    savePackage += "CName: " + getName() + "\n";
    savePackage += getText() + "\n";
    return savePackage;
}
}

And my attempt to save the pojo:

fileName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //doc identifier stuff
            SharedPreferences sharedPrefs = getSharedPreferences("DOC_INT",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPrefs.edit();
            doc_id++;
            editor.putInt("doc_id",doc_id);
            editor.commit();
            //dataPojo stuff
            SharedPreferences sharedSavePrefs = getSharedPreferences("DATA"+doc_id,Context.MODE_PRIVATE);
            SharedPreferences.Editor save_editor = sharedSavePrefs.edit();


            dataPojo.setNotes(save_text_notes);
            dataPojo.setName(save_text_class_name);
            dataPojo.setFileName(file_name);











        }
    });

The reason why I have a doc identifier int is because I plan on having the user save multiple dataPojos, and I figured this would be the best way to identify/retrieve them later in another activity.The current problem I am facing is SharedPreferences will not allow me to store an object. How can I save each pojo the user creates internally with a specific identifier? Thanks!

Ethan
  • 225
  • 2
  • 13
  • 2
    You never explained your problem. If you want sample code I'm guessing there are plenty of examples out there. – keyser Jan 25 '15 at 20:48

2 Answers2

1

Store them in a database using some ORM freeware library such as Sugar ORM

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • All that overhead to save a simple POJO? Why? – Simon Jan 25 '15 at 20:50
  • @Simon Because I just learned what a POJO was yesterday, and therefore do not know any easier way to create/save one. Can you help? – Ethan Jan 25 '15 at 20:53
  • Serialize a pojo and use ObjectInputStream/ObjectOutputStream to save it into a db if you don't want to use an ORM as it was said in the answer or use it (I personally like OrmLite http://ormlite.com/). – Sergey Benner Jan 25 '15 at 21:06
  • Better not serialize your objects, there are all kinds of problems with that. Use some ORM if it's acceptable. – Alexander Kulyakhtin Jan 25 '15 at 21:13
1

There is no straight way to save objects (POJO's) in SharedPreferences. However, there is another possibility to store POJO as JSON string (see this answer). Using this approach you just need to think about unique keys for them.

Community
  • 1
  • 1
yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • So now my question is whether or not I have to create a new pojo for every save, because after the data is saved the values in my pojo will be reset back to default so the user can create another save if they need to. – Ethan Jan 25 '15 at 21:10
  • Yes, you need to create a new POJO to save them in SharedPreference. The most important thing is that they need to have different keys. If they will have unique keys, they will not be overwritten. – yyunikov Jan 25 '15 at 21:14
  • I think that the easier/ more efficient way to do this for me is to store them in an Sql database. Your thoughts? – Ethan Jan 25 '15 at 21:18
  • 1
    If you have many of them (or that is possible), then it will be much better to store them in SQLite db. But if not, you can save them in SharedPreferences. – yyunikov Jan 25 '15 at 21:20
  • Yes, there will most certainly be many of them. SQL will be the way I go. Thanks everyone – Ethan Jan 25 '15 at 21:21
  • Ok, if this was useful, please press +1 to my answer :) – yyunikov Jan 25 '15 at 21:22