0

I am trying to set multiple values in hashmap in my android app. Some values are String and one value I want to send is an array of objects I created OnlineTestResultPOJO. Following is the code which I want to implement.

OnlineTestResultPOJO[] records = db.resultDao().getRecords();
HashMap<String, String> map = new HashMap<>();
map.put("dbname",dbname);
map.put("class",clas);
map.put("rollno",rollno);
map.put("access_token",accessToken);
map.put("records_arr", records);

Obviously it is giving error in the last line as it is not String, but is it possible to set all of these things in one HashMap? I tried wrapping it with String.valueOf(records), but that spoils the working of object producing unexpected results. Any alternate solution to achieve this?

sman
  • 3
  • 4

3 Answers3

0

Create map as below:

HashMap<String, Object> map = new HashMap<>();
Ganesh Modak
  • 171
  • 9
0

Just declare your map like this:

HashMap<String, Object> map = new HashMap<>();

e.g Object type for the value.

Sharofiddin
  • 340
  • 2
  • 14
0

You can serialize Object to String and save it to HashMap as a String.

// serialize the object
 try {
     ByteArrayOutputStream bo = new ByteArrayOutputStream();
     ObjectOutputStream so = new ObjectOutputStream(bo);
     so.writeObject(myObject);
     so.flush();
     serializedObject = bo.toString();
 } catch (Exception e) {
     System.out.println(e);
 }

 // deserialize the object
 try {
     byte b[] = serializedObject.getBytes(); 
     ByteArrayInputStream bi = new ByteArrayInputStream(b);
     ObjectInputStream si = new ObjectInputStream(bi);
     MyObject obj = (MyObject) si.readObject();
 } catch (Exception e) {
     System.out.println(e);
 }