I want to store hashmap to my android application that when restart ,it shows last saved values of hashmap.
HashMap HtKpi=new HashMap&l
Serialize it and save it in shared preferences or in a file. Whether you can do this, of course, depends on the data types being mapped from and to. (This won't work, for instance, if you try to serialize a View.)
Example:
//persist
HashMap<String, Integer> counters; //the hashmap you want to save
SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
for (String s : counters.keySet()) {
editor.putInteger(s, counters.get(s));
}
editor.commit();
//load
SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE);
HashMap<String, Integer> map= (HashMap<String, Integer>) pref.getAll();
for (String s : map.keySet()) {
Integer value=map.get(s);
//Use Value
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
You can store it here.... This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state.
And then you can retireve it back from here,,,
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState...
I think that this will help
using Raghav's I created a complete working example:
public class AppCache {
public static HashMap<String, String> hashMap = null;
/* REPOSITORY NAME */
public static final String REPOSITORY_NAME = "HMIAndroid_System_Settings";
/* SETTINGS */
public static final String SETTING_PLANNED = "DATA_PLANNED";
public static final String SETTING_ACTUAL = "DATA_ACTUAL";
public static final String SETTING_ETA = "DATA_ETA";
public static final String SETTING_OR = "DATA_OR";
public static final String SETTING_LINEID = "LINEID";
public static final String SETTING_SERVERIP = "SERVERIP";
public static void LoadSettings(Context context) {
SharedPreferences pref = context.getSharedPreferences(REPOSITORY_NAME, Context.MODE_PRIVATE);
hashMap = (HashMap<String, String>) pref.getAll();
if(hashMap == null) {
hashMap = new HashMap<String, String>();
}
}
public static void SaveSettings(Context context) {
if(hashMap == null) {
hashMap = new HashMap<String, String>();
}
//persist
SharedPreferences pref = context.getSharedPreferences(REPOSITORY_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
for (String s : hashMap.keySet()) {
editor.putString(s, hashMap.get(s));
}
editor.commit();
}
}
Then in your onCreate load it:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//load settings
AppCache.LoadSettings(getApplicationContext());
Log.d("onCreate","My LineID=" + AppCache.hashMap.get(AppCache.SETTING_LINEID));
Log.d("onCreate","Dest ServerIP=" + AppCache.hashMap.get(AppCache.SETTING_SERVERIP));
}
at any time anywhere in your app update settings:
AppCache.hashMap.put(AppCache.SETTING_LINEID, "1");
AppCache.hashMap.put(AppCache.SETTING_SERVERIP, "192.168.1.10");
in your onDestroy save settings to cache:
@Override
protected void onDestroy() {
//save settings
AppCache.SaveSettings(getApplicationContext());
}
You could serialize it to json and store the resulting string in the preferences. Then when application restarts get the string from preferences and deserialize it.
EDIT :
To do so you can use Google Gson for example.
You will need to wrap your map in a class:
public class MapWrapper {
private HashMap<Integer, String> myMap;
// getter and setter for 'myMap'
}
To store the map:
Gson gson = new Gson();
MapWrapper wrapper = new MapWrapper();
wrapper.setMyMap(HtKpi);
String serializedMap = gson.toJson(wrapper);
// add 'serializedMap' to preferences
To retrieve the map:
String wrapperStr = preferences.getString(yourKey);
MapWrapper wrapper = gson.fromJson(wrapperStr, MapWrapper.class);
HashMap<Integer, String> HtKpi = wrapper.getMyMap();
I have a solution. Same thing i had done in my application where i wanted to store the name of states as key and state abbreviation as value. For that declared an string array in "res/values/string.xml". Here is the code used to declare an array :
<string-array name="array_string">
<item>yourKey1;yourValue1</item>
<item>yourKey2;yourValue2</item>
<item>yourKey3;yourValue3</item>
</string-array>
And after this, where you want to instantiate your hashmap, do this :
HashMap<String, String> map = new HashMap<String, String>();
Resources res = getResources();
String[] stringArray = res.getStringArray(R.array.array_string);
//R.array.array_string is the name of your array declared in string.xml
if(stringArray == null || stringArray.length == 0) return;
for(String string : stringArray) {
String[] splittedString = string.split(";");
if(splittedString.length > 0) {
String key = splittedString[0];
String value = splittedString[1];
map.put(key, value);
}
}
Now you have your HaspMap instance ready for use. This is simple way of doing this which i prefer.