Android saving hashmap to phone memory



While working on my current project,there was need to save HashMap to phone memory.Lots of example demonstrated saving ArrayList with String object,but my need was to save HashMap with custom objects.The format of my HashMap was

HashMap<String,SettingsValue>

And SettingsValue.java is like this

public class SettingsValue implements Serializable{

 private static final long serialVersionUID = -8631648651074307073L;
 public int intValue = -1;
 public String stringValue =null;
 public Boolean booleanValue =null;

 public Object fetchSomeValue(){
    if(intValue != -1){
      return intValue;
    }else if(stringValue != null){
      return stringValue;
    }else if(booleanValue != null){
      return booleanValue;
   }
     return null;
 }
}

For demonstration purpose let’s consider a layout having two buttons,on click of first button,the HashMap is stored to the memory and on click of second button,the same HashMap is restored from the phone memory.
Let our layout be main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Save"
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Fetch"
android:id="@+id/fetchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

And lastly,our Activity class where ,there is store and fetch of HashMap of custom objects

public class ReadWriteActivity extends Activity {
private Map<String, SettingsValue> settingValueHolder =new HashMap<String, SettingsValue>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
populateHashMap();
Button saveButton=(Button)findViewById(R.id.saveButton);
Button fetchButton=(Button)findViewById(R.id.fetchButton);

saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
saveToPhoneMemory();
}
});

fetchButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
fetchFromPhoneMemory();
}
});
}

/*method to save HashMap to phone memory*/

protected void saveToPhoneMemory() {
String filename ="file.bin";
FileOutputStream fos;
try {
fos =openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
for(Map.Entry<String, SettingsValue>entrySet:settingValueHolder.entrySet()){
out.writeBytes(entrySet.getKey());
SettingsValue setValue = entrySet.getValue();
out.writeObject(setValue);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/*method to retrieve stored HashMap from phone memory*/

protected void fetchFromPhoneMemory() {
try {
FileInputStream fIn =openFileInput("file.bin");
InputStreamReader isr = new InputStreamReader(fIn);
ObjectInputStream in = new ObjectInputStream(fIn);
HashMap<String,SettingsValue> newHashMap = new HashMap<String, SettingsValue>();
while(in.available()>0){
newHashMap.put(in.readLine(), (SettingsValue)in.readObject());
}
for(Map.Entry<String, SettingsValue>entrySet:newHashMap.entrySet()){
Log.i("KEY",entrySet.getKey());
SettingsValue setValue = entrySet.getValue();
Log.i("VALUE",String.valueOf(setValue.fetchSomeValue()));
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void populateHashMap(){
SettingsValue setValue = new SettingsValue();
setValue.intValue =20;
settingValueHolder.put("Integer 20 value", setValue);
setValue = new SettingsValue();
setValue.booleanValue =true;
settingValueHolder.put("Boolean true", setValue);
setValue = new SettingsValue();
setValue.stringValue ="Hello there buddy";
settingValueHolder.put("String some value", setValue);
setValue = new SettingsValue();
setValue.booleanValue =false;
settingValueHolder.put("Boolean false", setValue);
}
}

So in this manner,we can save and retrieve HashMap with custom objects to and from phone memory.Hope this could help someone. 😉

Advertisements

2 thoughts on “Android saving hashmap to phone memory

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s