我有视图对象,用户可以动态地添加到布局中,该布局代表如下图所示的地图。
http://web.cecs.pdx.edu/~sheard/course/Cs163/Graphics/graph2.jpg
唯一的问题是用户需要能够保存数据,所以我想将视图对象存储在SQLite数据库中,该数据库保存到recyclerView中。当用户点击recyclerView上的项目时,它应该能够加载数据并填充上次保存时的布局...
为了序列化视图对象,我尝试过GSON解析对象,但是由于" StackOverFlowError:AsyncTask中的堆栈大小为1036KB而没有工作......"。
//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);
}
}
我开始得出这样的结论:GSON不能用于复杂的对象或太大的对象,所以有什么我做错了或者是否有另一种方法可以将对象状态序列化以存储在SQLite数据库中并在以后检索。
感谢