I want to store hashmap to my android application that when restart ,it shows last saved values of hashmap.
HashMap HtKpi=new HashMap&l
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 :
- yourKey1;yourValue1
- yourKey2;yourValue2
- yourKey3;yourValue3
And after this, where you want to instantiate your hashmap, do this :
HashMap map = new HashMap();
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.