我想将数据从db显示到条形图(Achartengine),我有两个字段date和bmi结果。日期是字符串格式,bmi是double / int。
这是我到目前为止所尝试的:
public class BMICalculatorDB {
public static final String KEY_BMIID = "bmi_id";
public static final String KEY_BMIDATA = "bmi_data";
public static final String KEY_BMIDATE = "bmi_date";
private static final String DATABASE_NAME = "DB_bmi";
private static final String DATABASE_TABLE = "tbl_bmi";
private static final int DATABASE_VERSION = 1;
private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_BMIID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_BMIDATA + " TEXT NOT NULL, " +
KEY_BMIDATE + " TEXT NOT NULL );"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL( "DROP TABLE IF EXISTS " + DATABASE_TABLE );
onCreate(db);
}
}
public BMICalculatorDB(Context c){
ourContext = c;
}
public BMICalculatorDB open(){
ourHelper = new DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry( String data, String date ) {
//, String date
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_BMIDATA, data);
cv.put(KEY_BMIDATE, date);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getBMIID() {
// TODO Auto-generated method stub
String[] column =
new String[]{ KEY_BMIID };
Cursor c =
ourDatabase.query(DATABASE_TABLE, column, null, null, null, null, null);
String result = "";
int iID = c.getColumnIndex(KEY_BMIID);
for ( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString(iID);
}
return result;
}
public String getBMIDataData(){
String[] column =
new String[]{ KEY_BMIDATA };
Cursor c =
ourDatabase.query( DATABASE_TABLE, column, null, null, null, null, null );
String result = "";
int iData = c.getColumnIndex( KEY_BMIDATA );
for ( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString( iData );
}
return result;
}
public String getBMIDateData(){
String[] column =
new String[]{ KEY_BMIDATE };
Cursor c =
ourDatabase.query( DATABASE_TABLE, column, null, null, null, null, null );
String result = "";
int iDate = c.getColumnIndex( KEY_BMIDATE);
for ( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString( iDate );
}
return result;
}
public void updateEntry( long lId, String mData, String mDate ) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put( KEY_BMIDATA, mData );
cvUpdate.put( KEY_BMIDATE, mDate );
ourDatabase.update( DATABASE_TABLE, cvUpdate, KEY_BMIID + " = lId", null );
}
public String getData(long l) {
// TODO Auto-generated method stub
return null;
}
public String getDate(long l) {
// TODO Auto-generated method stub
return null;
}
public XYMultipleSeriesDataset getDemoDataset(String title) {
String[] column =
new String[]{ KEY_BMIDATA };
Cursor c = ourDatabase.query( DATABASE_TABLE, column, null, null, null, null, null );
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
TimeSeries series = new TimeSeries("Line1");
TimeSeries series2 = new TimeSeries(title);
getBMIDataData();
while (!c.isAfterLast()) {
int date = c.getInt((Integer) c.getColumnIndexOrThrow("bmi_date"));
int weight = c.getInt((Integer) c.getColumnIndexOrThrow("bmi_data"));
series2.add(weight, date);
c.moveToNext();
}
c.close();
dataset.addSeries(series);
dataset.addSeries(series2);
return dataset;
}
public Intent getIntent(Context context) {
//Lager TimeSeries for den første linja
XYMultipleSeriesDataset dataset = getDemoDataset("Line1");
//Kode for render
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
//Optimalisering linje1
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.YELLOW);
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(true);
// Optimalisering linje2 husk rekke følgen
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.BLUE);
renderer2.setPointStyle(PointStyle.SQUARE);
renderer2.setFillPoints(true);
//Legger til render seriene
mRenderer.addSeriesRenderer(renderer);
//Optimalisering grafen
mRenderer.setChartTitle("Test");
mRenderer.setZoomEnabled(true);
mRenderer.setZoomButtonsVisible(true);
mRenderer.setBackgroundColor(Color.BLACK);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setXTitle("Dager");
mRenderer.setShowGrid(true);
mRenderer.addSeriesRenderer(renderer2);
Intent intent = ChartFactory.getLineChartIntent(context, dataset,
mRenderer, "Line Graph Title");
return intent;
}
}
但我在第28行遇到错误:
Cursor c = dbSqlite.query( DATABASE_TABLE, column, null, null, null, null, null );
和
XYMultipleSeriesDataset dataset = getDemoDataset("Line1");
我真的很困惑。真的很感激任何帮助。
错误:
11-11 02:33:06.542: E/AndroidRuntime(344): FATAL EXCEPTION: main
11-11 02:33:06.542: E/AndroidRuntime(344): java.lang.NullPointerException
11-11 02:33:06.542: E/AndroidRuntime(344): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
11-11 02:33:06.542: E/AndroidRuntime(344): at com.fps.iHealthFirst.calculators.LineGraph.getDemoDataset(LineGraph.java:53)
11-11 02:33:06.542: E/AndroidRuntime(344): at com.fps.iHealthFirst.calculators.LineGraph.getIntent(LineGraph.java:87)
11-11 02:33:06.542: E/AndroidRuntime(344): at com.fps.iHealthFirst.calculators.BMICalculator.onClick(BMICalculator.java:181)
11-11 02:33:06.542: E/AndroidRuntime(344): at android.view.View.performClick(View.java:2485)
11-11 02:33:06.542: E/AndroidRuntime(344): at android.view.View$PerformClick.run(View.java:9080)
11-11 02:33:06.542: E/AndroidRuntime(344): at android.os.Handler.handleCallback(Handler.java:587)
11-11 02:33:06.542: E/AndroidRuntime(344): at android.os.Handler.dispatchMessage(Handler.java:92)