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?

4
  • What is the best method? A matter of opinion Commented Nov 29, 2016 at 0:12
  • What do you think Piodo? Commented Nov 29, 2016 at 0:12
  • Do you have to store the whole object, or can you store the Bluetooth addresses as a string list? Commented Nov 29, 2016 at 0:38
  • I would like to store whole object. Someone just posted a method that helped me. Commented Nov 29, 2016 at 14:55

3 Answers 3

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()));
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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);

Comments

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.

3 Comments

This assumes BeaconDevice is serializable
@cricket_007 Gson doesn't require objects to implement Serializable.
There's a difference between what I meant of "ability to be serialized" and the Java Serializable interface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.