My projects up until never needed data saved so this is the first time i have ever saved data. What i am trying to do is save data from an array list thats based off a class. Ive seen several people ask this with several different answers but i seem to be missing something. This is what the code is stripped down after all my attempts. Im hoping someone can help me on what to do for saving and loading with this info. Almost forgot this is a profile save of the app so there can be more then one file if the user chooses to.
//in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//in java file
private ArrayList<otherclass> otherClass=new ArrayList<otherclass>();
class saveData
{
static private final int version=101002;
private String title;
private int[] Int1=new int[3];
private int[] Int2=new int[3];
private int[] Int3=new int[3];
private int Int;
}
class otherclass
{
//all the data goes here to similar named variables
}
One way is to have the class you are adding to the ArrayList implement serializable. If your class is only made up of objects that also implement serializable then you are done (this is most likely the case), just add implements Serializable like this:
Otherwise you will need to add the two below methods to your class
Add implements Serialible lets ObjectOutStream know that it can serialize your data for storage:
Then, you can implement the below methods to save and open your data, see comments for what each step does…
Finally, both of the file input/output streams can be wrapped in a bufferedinputstream / bufferedoutput stream, but I have found with small files it doesn’t affect performance much. That could be accomplished by
okay, below is a full activity file to demostrate this and has been tested with version 2.1…the only thing you need to change is the package name to match your project…note that this changes the variables in saveData to package from private, if you want to keep them private, which you probably should, you need to implement setters/getters, but the below code should help you understand saving/loading objects…
As I mentioned in my last post, the below updated methods for load and save improve the performance…