根据SQLite数据库列绘制图形

时间:2013-07-23 06:35:06

标签: android sqlite graph

我想根据SQLite数据库列中的数据创建一个图表。我已经安装了achartengine.jar,我也知道如何在数组中的硬编码值上生成图表。 但我无法弄清楚如何使用Sqlite数据。 任何人都可以帮助我链接到教程如何实现这一点,或者可以帮助我将数据库列转换为我可以使用的数组。

感谢您的帮助!!

public class ReportGraph extends GraphicalActivity
{

    double[] fQty;
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        getIntent();

    }

    public Intent getIntent(Context context)
    {

        FuelStoredInfo reportInfo =new FuelStoredInfo(this);
        reportInfo.open();
        fQty=reportInfo.getReportData(this);
        reportInfo.close();



        CategorySeries series = new CategorySeries("Reports");
        for(int i = 0; i< fQty.length; i++)
        {
            series.add("Report"+(i+1), fQty[i]);

        }   



        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(series.toXYSeries());
        //dataset.addSeries(series1.toXYSeries());

        //customize bar 1
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setDisplayChartValues(true);
        renderer.setChartValuesSpacing((float)0.5);
        renderer.setColor(Color.BLUE);




        XYMultipleSeriesRenderer rRenderer = new XYMultipleSeriesRenderer();
        rRenderer.addSeriesRenderer(renderer);
        rRenderer.setChartTitle("Reports");
        rRenderer.setXTitle("Date");
        rRenderer.setYTitle("Prices");


        Intent i =ChartFactory.getBarChartIntent(context, dataset, rRenderer, Type.DEFAULT);

    return i;

    }


    public double[] getfQty() {
        return fQty;
    }

    public void setfQty(double[] fQty) {
        this.fQty = fQty;



}

FuelStoredInfo.java(数据库)

public FuelStoredInfo open() throws SQLiteException
    {
        OpenDBHelper ourHelper = new openDBHelper(ourContext);
               SQLiteDatabase   ourDatabase = ourHelper.getWritableDatabase();
        return this;

    }

    public double[] getReportData(ReportGraph reportGraph)
        {
            // TODO Auto-generated method stub
            String [] columns = new String[]{KEY_ROW_ID, KEY_KM, KEY_FUEL_QTY, KEY_FUEL_PRICE, KEY_TOTAL_COST, KEY_MILEAGE, KEY_DATE,KEY_TANK_FULL};

             double[] reportList ;
                int i = 0;

                String selectQuery = "SELECT  * FROM fuel_table";

               reportCursor = ourDatabase.rawQuery(selectQuery, null);
                int count = reportCursor.getCount();
                reportList = new double[count];

                if (reportCursor.moveToFirst()) 
                {
                    do {
                        reportList[i] = reportCursor.getDouble(2);
                        i++;

                        } while (reportCursor.moveToNext());
                }


                return reportList;
        }

错误日志 - &gt;

07-23 13:16:11.393: E/AndroidRuntime(991): java.lang.NullPointerException
07-23 13:16:11.393: E/AndroidRuntime(991):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
07-23 13:16:11.393: E/AndroidRuntime(991):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
07-23 13:16:11.393: E/AndroidRuntime(991):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-23 13:16:11.393: E/AndroidRuntime(991):  at com.example.fuelcheck.FuelStoredInfo.open(FuelStoredInfo.java:104)
07-23 13:16:11.393: E/AndroidRuntime(991):  at com.example.fuelcheck.ReportGraph.getIntent(ReportGraph.java:36)

1 个答案:

答案 0 :(得分:1)

以下是您可以使用的示例..

public String[] getAllContacts() {
    String[] contactList ;
    int i = 0;
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;//You fetch query

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    int count = cursor.getCount();
    contactList = new String[count];
    // looping through all rows and adding to array
    if (cursor.moveToFirst()) {
        do {
            contactList[i] = cursor.getString(0);//getting data from column 0
            i++;
            // Adding contact to list
            } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

我从第0列获取数据,但您可以获取任何列...

希望这会有所帮助..