通过游标从SQLite数据库中检索日期

时间:2013-08-14 07:49:53

标签: android achartengine

我试图使用achartengine绘制图形,我需要从数据库中获取日期数组,以便我可以将它与TimeSeries一起使用。 但是我无法使用游标从数据库中检索日期。 请帮助

heres my code:-

    public Date[] getReportDates(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};

            String selectQuery = "SELECT _date FROM fuel_table";
            reportCursor = ourDatabase.rawQuery(selectQuery, null);


            Date[] reportList=null ;
            int i = 0;
            int count = reportCursor.getCount();

            reportList = new Date[count];

            if(reportCursor.moveToFirst())
            {
                do
                {

                    reportList[i] =  (Date)reportCursor.getLong(0); // ERROR here
                    i++;
                }while(reportCursor.moveToNext());

            } 
            reportCursor.close();
            return reportList;

        }   

code for line chart:-

double[] fPrice;
    double[] fMileage;
    Date[] fDates;

    XYMultipleSeriesRenderer rRenderer;

    XYMultipleSeriesDataset dataset;
    XYSeriesRenderer priceRenderer, mileageRenderer,dateRenderer;

    public Intent getIntent(Context context)
    {

        FuelStoredInfo reportInfo =new FuelStoredInfo(context);
        reportInfo.open();
        fPrice=reportInfo.getReportData(this);
        fMileage = reportInfo.getReportMileage(this);
        fDates = reportInfo.getReportDates(this);
        reportInfo.close();


                       TimeSeries fPriceseries = new TimeSeries("Fuel prices");
                       for(int i=0;i<fDates.length;i++)
                       {
                           fPriceseries.add(fDates[i], fPrice[i]);
                       }
                       TimeSeries fMileageSeries = new TimeSeries("Mileage");
                        for(int i=0;i<fDates.length;i++)
                        {
                            fMileageSeries.add(fDates[i],fMileage[i]);
                        }
                        // Our second data

                        dataset = new XYMultipleSeriesDataset();
                        dataset.addSeries(fPriceseries);
                        dataset.addSeries(fMileageSeries);

enter image description here

1 个答案:

答案 0 :(得分:1)

你不能简单地将长期投射到日期。尝试使用Date(long)构造函数。

reportList[i] =  new Date(reportCursor.getLong(0));