2013-01-02 50 views
0

我有以下活動機器人如何將對象傳遞給一個新的類

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); 
    ... 
} 


} 

我的問題是如何使數據從Activity中可用的光標移動到第二個類以便繪製圖形?

非常感謝

+2

在'BmiGraph'添加一個setter方法和'BmiHistory' –

+0

由於K-BALLO調用它,我嘗試的 - 但我不知道語義很好 – Herb

+0

它大多隻是語法,而不是語義。您將不得不學習_Java_如果你想編寫_Android_應用程序。 –

回答

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); 
    ... 
} 


} 
相關問題