android - How do you store the contents of a POJO in the preference store? -
i looking find way store value of pojo (containing strings, booleans, ints) in preference variable can retrieve key used when store it. pojo contains many string / boolean / int attributes, don't want store them each individually. problem i'm running preference variable types string, boolean, float, , int. there way convert pojo string retrieve per it's key , convert pojo, sort of casting it> 1) populate pojo 2) convert pojo string 3) store string in preference store key value (normal preference store stuff) 4) when needed, retrieve data preference store string , convert pojo.
not elegant solution but:
bytearrayoutputstream outputstream = new bytearrayoutputstream(); objectoutputstream = new objectoutputstream(outputstream); objectoutputstream.writeobject(instance); objectoutputstream.flush(); val = base64.encodebase64string(outputstream.tobytearray()));
to deserialise:
byte[] data = base64.decodebase64(datastr); objectinputstream objectinputstream = null; try { bytearrayinputstream inputstream = new bytearrayinputstream(data); objectinputstream = new objectinputstream(inputstream); return (t)objectinputstream.readobject(); } { if (objectinputstream != null) objectinputstream.close(); }
where t object type.
Comments
Post a Comment