1

Basically I have an app that fetches some information from Facebook, and that info can be regularly updated. I want to save that information in the phone so that when the user does not have an internet connection he can still see the latest fetch.

The information is saved in a

ArrayList<HashMap<String, String>>

What should I use?

The amount of information to save is small. 7 entries in the HashMap and at most 15 in the ArrayList. I use this datatype because I display it in a ListView.

Again that info must be saved even is the app is closed.

Regards

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
danielnovais92
  • 633
  • 3
  • 8
  • 19

3 Answers3

2

you can write it to a json or xml file , and load that file when app started

akbarian
  • 51
  • 8
1

Have you looked at serialisation before? I find it very useful for this type of thing.

What is object serialization?

You can serialise out your data into an arbitrary file on the device SD card for example, then just read it back in on startup. I've used it for storing data in games, e.g. a save file.

Peter Flower
  • 363
  • 2
  • 20
1

I think the best way is to save it to SharedPreferences. An easy way is to convert this object to JSON String and store that String in the SharedPreferences, and then when needed, get JSON string back and convert it back to your object. The library that does it nicely is Google's gson library. If you are using Gradle, import it like this:

dependencies {
    ...
    compile 'com.google.code.gson:gson:2.2.+'
    ...
}

then, you can use this simple class to convert objects to/from String

public class JsonHelper {
    public static Gson gson = new GsonBuilder()
            .setPrettyPrinting().create();

    public static Object getObject(String jsonString, Type classType){
        return gson.fromJson(jsonString, classType);
    }

    public static String getJsonString(Object object){
        return gson.toJson(object);
    }
}

then, you can do this:

 //to get JSON string from your object
        ArrayList<HashMap<String, String>> yourList = ...;
        String JSONString = JsonHelper.getJsonString(yourList);
        //save string to shared preference

//to get your object from JSON string
        //get JSON string from shared prefs
        String yourJsonString = ...;
        Type t = new TypeToken<ArrayList<HashMap<String, String>>>() { }.getType();
        ArrayList<HashMap<String, String>> yourList = (ArrayList<HashMap<String, String>>)JsonHelper.getObject(yourJsonString, t);

here is some info about SharedPreferences and some info on how to use SharedPreferences, its really easy.

Then, you can add these methods to your Activity

public class YourActivity extends Activity{
    public static final String  KEY_PREFS = "com.your_app_name";
    public static final String  KEY_DATA = "your_data";
    ...
    public static void saveDataToPrefs(String json){
        getSharedPreferences(KEY_PREFS, Context.MODE_PRIVATE).edit().putString(KEY_DATA, json).commit();
    }

    public ArrayList<HashMap<String, String>> getDataFromPrefs(){
        Type t = new TypeToken<ArrayList<HashMap<String, String>>>() { }.getType();

        return (ArrayList<HashMap<String, String>>)JsonHelper
        .getObject(getSharedPreferences(KEY_PREFS, Context.MODE_PRIVATE)
        .getString(KEY_DATA, ""), t);
    }
}

Please note this is not the best way to save the info in the app persistantly, as this method can produce unexpected bugs, like in case the final JSON string needs to be larger then the String object in the Android system. The best way is to have a database.

Community
  • 1
  • 1
C0D3LIC1OU5
  • 8,600
  • 2
  • 37
  • 47