android如何将对象传递给新类

时间:2013-01-02 22:31:08

标签: android oop

我有以下活动

public class BmiHistory extends Activity {


public Cursor cursor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.bmihistory);
    final DatabaseHandler databaseOpen = new DatabaseHandler(this);
    final SQLiteDatabase db = databaseOpen.getReadableDatabase();

    Cursor c1 = db.rawQuery("select * from bmi", null);
    startManagingCursor(c1);
    c1.moveToFirst();
    this.cursor = c1; 
    while(!c1.isAfterLast()) {
        System.out.println(c1.getString(2));
        c1.moveToNext();
    }
    c1.close();


    BmiGraph drawView = new BmiGraph(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);

}


}

我从此活动中调用了以下类

public class BmiGraph extends View {

Paint paint = new Paint();


public BmiGraph(BmiHistory context) {
    super(context);
    // TODO Auto-generated constructor stub
    System.out.println(context.toString());
}

public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    ...
}


}

我的问题是如何将活动中光标的数据提供给第二类才能绘制图形?

非常感谢

1 个答案:

答案 0 :(得分:0)

您可以修改BmiGraph类的构造函数以接受您的光标作为输入(见下文)。然后,您将可以从BmiGraph类中访问您的光标。

public class BmiGraph extends View {

Paint paint = new Paint();

Cursor _someCursor;    

public BmiGraph(BmiHistory context, Cursor someCursor) {
    super(context);
    // TODO Auto-generated constructor stub
    System.out.println(context.toString());

    _someCursor = someCursor
}

public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    ...
}


}