Search code examples
javaandroidpojo

How to Save A POJO Object In Internal Storage


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!


Solution

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