Reputation: 8981
I need to have an int
array which holds several Integer
values. This array should not be clear when the user terminates the application. What I am going to use this for:
The array cannot be deleted after the user exits the application, I need this for generating a "subset" of a Unique ID.
So far I've tried using a SQLite
database for holding this information, but I think this is to complex way to do it.
Any suggestions?
Upvotes: 0
Views: 94
Reputation: 1132
Try serializing the array and storing it in a File. (you can use your own logic or use some other custom library like GSON
for this purpose)
Next time when the application is launched you can take the file content
and reconstruct the array back.
Upvotes: 2
Reputation: 54322
Well, you can use shared preference which is preferably a less complexion. But the problem is that you can't store a array object. Only primitive types can be stored.
But still here is a example which uses JsonArray to do the same. But it is not a advisory one to do though,.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
JSONArray arr = new JSONArray();
arr.put(12);
arr.put(-6);
prefs.edit().putString("key", arr.toString());
prefs.edit().commit();
// read
try {
arr = new JSONArray(prefs.getString("key", "{}"));
arr.getInt(1); // -6
} catch (JSONException e) {
e.printStackTrace();
}
Taken form here.
Upvotes: 2