I have view objects which the user can dynamically add to a layout which represents a map like the image shown below.
http://web.cecs.pdx.edu/~sheard/course/Cs163/Graphics/graph2.jpg
The only problem is that the user needs to be able to save the data, so I thought to store the view objects in an SQLite database which saves into a recyclerView. When the user clicks on the item on recyclerView, it should be able to load the data and populate the layout as it was from when it was last saved...
To serialize the view objects I have tried GSON parsing of objects, but didnt work due to "StackOverFlowError: Stack size 1036KB in AsyncTask... ".
//Convert view object into JSON string
class JSONConversion extends AsyncTask<NodeView, String, String>{
@Override
protected String doInBackground(NodeView... params) {
Gson gson = new Gson();
String json = gson.toJson(params[0]);
System.out.println("DOINBACKGround " + json);
return json;
}
}
//method to test whether the conversion worked
private void launchGSON(){
NodeView node = new NodeView("GAA",23,this); //test view object
new JSONConversion().execute(node); // load separate thread and
//convert view object to JSON string, if successful should display the json string in the logcat
}
//Custom View object class
public class NodeView extends View {
private String task;
private int time;
//bitmap image to be drawn on canvas
private transient Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.node_icon);
public NodeView(String task, int time, Context mContext) {
super(mContext);
this.task = task;
this.time = time;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//bitmap drawn on canvas
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(150, 150);
}
}
I am starting to come to the conclusion that GSON will not work with a complex object or too large of an object, so is there something Im doing wrong or is there another way i can serialize the object state to store in SQLite database and retrieve later on.
thanks
Don't use gson for large objects. Instead, your objects should implement Serializable interface. Look here (good tutorial): http://www.javaworld.com/article/2076120/java-se/flatten-your-objects.html
Alternatively, you could use SnappyDb: https://github.com/nhachicha/SnappyDB/blob/master/README.md
It's faster than sqlite for large and complicated objects and you can store any pojo object without making it serializable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.