1
回答
求助:从sql数据库查询取值代入图表参数无法运行
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

Rt,小弟是菜鸟,刚开始学习android编程。目前被这个问题所困扰,代码如下:

package com.zzz.moneygo;


import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;


import com.zzz.moneygo.db.MoneyOpenHelper;


import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;


public class AChartExample {

// 数据库查询取值
private  MoneyOpenHelper helper;

public  Double getCount() {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery(
"select sum(money) from Money1 where IO = ?",
new String[] { "收入" });
cursor.moveToFirst();
Double count = cursor.getDouble(0);
cursor.close();
db.close();
return count;


}





// 图表部分
public Intent execute(Context context) {




int[] colors = new int[] { Color.RED, Color.YELLOW, Color.BLUE };
DefaultRenderer renderer = buildCategoryRenderer(colors);
CategorySeries categorySeries = new CategorySeries("Vehicles Chart");
categorySeries.add("cars ",getCount()); // 问题在这里,数据库取出的值代入这里不报错,但是运行的时候就会“停止工作”。LogCat提示“Could
// not execute method of the
// activity.
categorySeries.add("trucks", 90000);
categorySeries.add("bikes ", 90000);
return ChartFactory.getPieChartIntent(context, categorySeries,
renderer, null);
}


protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();


for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}


}

小弟怀疑是getCount()那里出的问题,但是怎么也想不明白。恳求各位高手大大指点迷津。再此先行谢过!

举报
赵金龙啊
发帖于4年前 1回/107阅
顶部