我在使用EditText中添加值时使用了绘制图表的教程,现在我正在更改它以将数据库中的值绘制到图表中。所以我需要创建一个循环来获取行中的所有数据并将它们逐行放入图表中。
这是来自DatabaseAdapter的查询
public String getWykresWaga2(int i) {
String[] columns = {DatabaseAdapter.WAGA};
Cursor cursor = db.query(DatabaseAdapter.TABLE_NAME, columns,DatabaseAdapter.UID+ "=" +i, null, null, null, null);
String WykresWaga = "";
if (cursor.moveToFirst()) {
do {
int index1 = cursor.getColumnIndex(DatabaseAdapter.WAGA);
WykresWaga = cursor.getString(index1);
return WykresWaga;
} while (cursor.moveToNext());
}
return WykresWaga;
}
public String getWykresBmi2(int i) {
String[] columns = {DatabaseAdapter.BMI};
Cursor cursor = db.query(DatabaseAdapter.TABLE_NAME, columns,DatabaseAdapter.UID+ "=" +i, null, null, null, null);
String WykresBmi = "";
if (cursor.moveToFirst()) {
do {
int index1 = cursor.getColumnIndex(DatabaseAdapter.BMI);
WykresBmi = WykresBmi + cursor.getString(index1);
return WykresBmi;
} while (cursor.moveToNext());
}
return WykresBmi;
}
public int getWykresId() {
String[] columns = {DatabaseAdapter.UID};
Cursor cursor = db.query(DatabaseAdapter.TABLE_NAME, columns, null, null, null, null, null);
int count = 0;
if (cursor.moveToFirst()) {
do {
cursor.getColumnIndex(DatabaseAdapter.BMI);
count = cursor.getCount();
} while (cursor.moveToNext());
}
return count ;
}
这是我的活动,我需要创建循环
String sx;
String sy;
int id;
id = bazadanych.getWykresId();
for (int i = 1; i == id; i++){
bazadanych = new DatabaseAdapter(getBaseContext());
bazadanych.open();
sx = bazadanych.getWykresWaga2(i);
sy = bazadanych.getWykresBmi2(i);
//x = bazadanych.getWykresBmi2();
double x = Double.parseDouble(sx);
// y = bazadanych.getWykresBmi2(); //error
double y = Double.parseDouble(sy);
// add a new data point to the current series
mCurrentSeries.add(x, y);
// repaint the chart such as the newly added point to be visible
mChartView.repaint();
bazadanych.close();}
我创建了一个循环但却一直都是错误。
01-23 15:25:41.635: E/AndroidRuntime(30670): FATAL EXCEPTION: main
01-23 15:25:41.635: E/AndroidRuntime(30670): java.lang.NullPointerException
01-23 15:25:41.635: E/AndroidRuntime(30670): at com.bialekwasiek.bmi.Motywacja$2.onClick(Motywacja.java:117)
01-23 15:25:41.635: E/AndroidRuntime(30670): at android.view.View.performClick(View.java:2408)
01-23 15:25:41.635: E/AndroidRuntime(30670): at android.view.View$PerformClick.run(View.java:8816)
01-23 15:25:41.635: E/AndroidRuntime(30670): at android.os.Handler.handleCallback(Handler.java:587)
01-23 15:25:41.635: E/AndroidRuntime(30670): at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 15:25:41.635: E/AndroidRuntime(30670): at android.os.Looper.loop(Looper.java:123)
01-23 15:25:41.635: E/AndroidRuntime(30670): at android.app.ActivityThread.main(ActivityThread.java:4633)
01-23 15:25:41.635: E/AndroidRuntime(30670): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 15:25:41.635: E/AndroidRuntime(30670): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 15:25:41.635: E/AndroidRuntime(30670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-23 15:25:41.635: E/AndroidRuntime(30670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-23 15:25:41.635: E/AndroidRuntime(30670): at dalvik.system.NativeStart.main(Native Method)