1

I have small list in program and I want to let user save it, instead of fill it every time app starts.

the list :

public static ArrayList<BeaconDevice> SavedBeacons;

Usually contains up to 5-10 BeaconDevice objects.

BeaconDevice is an object containing Beacon object (from AltBeacon library) and name in String format.

What is the best method?

Piodo
  • 616
  • 4
  • 20

3 Answers3

3

The essence of the question is about serializing and persisting objects, then later deserializing them. There are many ways to do this, and the more robust and complex solutions often involve a database, typically SQLite on Android, and some Object Relational Mapping framework.

However, such a robust solution may be overkill in this case, since the use case is for serializing and saving a very small list of objects, each of which have a limited number of fields. The most common way to save small chunks of data on Android is in Shared Preferences. You can get a reference to the object that manages these with:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

Once you have this, you save Strings to persistent storage with code like this:

SharedPreferences.Editor editor = preferences.edit();
editor.putString("savedBeacons", savedBeaconsAsString);
editor.apply();

Then load them back like this:

String savedBeaconsAsString = preferences.getString("savedBeacons", null);

Of course, the above is only saving a single String to persistent storage. How do you convert a list of objects to String, and how do you convert a String to a list of objects. This is called serialization.

There are lots of ways to do this including converting to and from JSON. One of the simplest methods is using Java Serialization APIs. The main disadvantage of these is that they can fail to deserialize (load back) your object if you later change the definition of the object in Java code after records has been saved.

Here is an example of converting an array of savedBeacons to and from a String, taken from this related answer. Note that the code below assumes that savedBeacons is an array, not an ArrayList as your code shows. I have shown this as an array for simplicity because ArrayList is not serializable.

// serialize
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(savedBeacons);
String savedBeaconsAsString = new String(Hex.encodeHex(out.toByteArray()));

// deserialize
ByteArrayInputStream in = new ByteArrayInputStream(Hex.decodeHex(savedBeaconsAsString.toCharArray()));
savedBeacons = (BeaconDevice[]) new ObjectInputStream(in).readObject()));
Community
  • 1
  • 1
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I saw this answer before, but I had problems with converting my arrayList to String. I dont have much time right now so I decided to use another answer using Gson method, that works fine. Thanks for reply. – Piodo Nov 29 '16 at 14:53
2

If it's a small list you could convert the list into a JSON string using the Gson library and save it as a String in your SharedPreferences. Refer to this answer https://stackoverflow.com/a/15011927/3090173 which essentially poses the same use case.

Community
  • 1
  • 1
crymson
  • 1,170
  • 7
  • 12
2

There is a library for this kind of stuff with easy solution

compile 'com.google.code.gson:gson:2.4'

So, by importing this you can directly save/retrive you arrylist like this ::

SAVE ::

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Type listOfBecons = new TypeToken<List<BeaconDevice>>() {}.getType();

String strBecons = new Gson().toJson(SavedBeacons, listOfBecons);
preferences.edit().putString("BECON_LIST", strBecons).apply();

RETRIEVE::

ArrayList<BeaconDevice> mSavedBeaconList = new Gson().fromJson(preferences.getString("BECON_LIST", ""), listOfBecons);
Anil Prajapati
  • 457
  • 3
  • 10