How can I call DataHelper from thread or paint-class with OnDraw?
Sorry about the in-concise title. I have an Android app in which I draw in a开发者_如何学编程 class that is run on a CanvasThread, to be kept away from the main GUI-thread.
But now when I want to draw rectangles based on data from a query to the DataHelper-class.
When I want to instantiate the DataHelper with DataHelper dh = new DataHelper(this);
i get the complaint that I need another constructor in DataHelper, that takes PanelChart as argument instead of Context. Why is this?
This is my PanelChart-class:
public class PanelChart extends SurfaceView implements SurfaceHolder.Callback {
private CanvasThread canvasthread ;
private SurfaceView sf;
private DataHelper dh ;
public PanelChart(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
canvasthread = new CanvasThread(getHolder(), this);
sf = (SurfaceView) findViewById(R.id.SurfaceView01);
setFocusable(true);
//getData(dh);
}
The getData(dh); is from where I want to call the method which calls DataHelper and retrieves the info needed to draw.
Should I make another constructor in DataHelper? Should I do the query in another class and send it to my PanelChart class?
Thanks!
I think you should use context parameter from PanelChart constructor, that's the context of Activity, in which you create PanelChart:
public class PanelChart extends SurfaceView implements SurfaceHolder.Callback {
private CanvasThread canvasthread ;
private SurfaceView sf;
private DataHelper dh ;
public PanelChart(Context context, AttributeSet attrs) {
super(context, attrs);
DataHelper dh = new DataHelper(context);
getHolder().addCallback(this);
canvasthread = new CanvasThread(getHolder(), this);
sf = (SurfaceView) findViewById(R.id.SurfaceView01);
setFocusable(true);
}
精彩评论