我有下一个愚蠢的情况:我想从数据库中读取数据并用这些数据绘制图表。
当我的代码是:
package com.example.gymforyou;
import java.sql.Date;
import java.text.SimpleDateFormat;
import org.achartengine.ChartFactory;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
public class LineGraph {
public Intent getIntent(Context context) {
final String LOG_TAG = "myLogs";
int countOfRecords = 0;
countOfRecords=3;
Date[] x = new Date[countOfRecords];
double y[] = new double[countOfRecords];
c.close();
db.close();
y[0]=10;
y[1]=20;
y[2]=34;
x[0]=Date.valueOf("2013-04-06");
x[1]=Date.valueOf("2013-05-06");
x[2]=Date.valueOf("2013-06-06");
TimeSeries series = new TimeSeries("Line1");
for (int i = 0; i < y.length; i++) {
series.add(x[i], y[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setDisplayChartValues(true);
renderer.setColor(Color.RED);
renderer.setFillPoints(true);
mRenderer.addSeriesRenderer(renderer);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.GRAY);
mRenderer.setShowGrid(true);
mRenderer.setZoomButtonsVisible(true);
Intent intent = ChartFactory.getTimeChartIntent(context, dataset,
mRenderer, "dd-MM-yyyy");
return intent;
}
} -一切都很好; 但是当我将其更改为:
package com.example.gymforyou;
import java.sql.Date;
import java.text.SimpleDateFormat;
import org.achartengine.ChartFactory;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
public class LineGraph {
public Intent getIntent(Context context) {
final String LOG_TAG = "myLogs";
DataBase db;
Cursor c;
int countOfRecords = 0;
db = new DataBase(context);
db.open();
c = db.getDateAndWeigth();
countOfRecords = c.getCount();
Log.d(LOG_TAG, "countOfRecords=" + countOfRecords);
Date[] x = new Date[countOfRecords];
double y[] = new double[countOfRecords];
int j = countOfRecords - 1;
if (c != null) {
if (c.moveToFirst()) {
do {
y[j] = c.getDouble(0);
x[j] = Date.valueOf(c.getString(1));
Log.d(LOG_TAG,
"Date" + c.getString(1) + " weigth= "
+ c.getDouble(0));
Log.d(LOG_TAG, "Record #" +j+ " Secsess");
j--;
} while (c.moveToNext());
}
} else
Log.d(LOG_TAG, "Cursor is null");
c.close();
db.close();
TimeSeries series = new TimeSeries("Line1");
for (int i = 0; i < y.length; i++) {
series.add(x[i], y[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setDisplayChartValues(true);
renderer.setColor(Color.RED);
renderer.setFillPoints(true);
mRenderer.addSeriesRenderer(renderer);
// mRenderer.setDisplayValues(true);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.GRAY);
mRenderer.setShowGrid(true);
// mRenderer.setXLabelsAlign(Align.CENTER);
// mRenderer.setXTitle("Date");
// mRenderer.setXLabels(countOfRecords);
;
mRenderer.setZoomButtonsVisible(true);
Intent intent = ChartFactory.getTimeChartIntent(context, dataset,
mRenderer, "dd-MM-yyyy");
return intent;
}
}
如果数据库中有 2 条记录,它会起作用,但是当有 3 条记录时,我会收到下一个错误:
我很惊讶,为什么它适用于 DB 中的两条记录,数组中的 3 条记录,但不适用于 DB 中的 3 条记录