2013-07-23 63 views
1

我想根據SQLite數據庫列中的數據創建圖形。我已經安裝了achartengine.jar,並且我也知道如何在數組中的硬編碼值上生成圖形。 但我無法弄清楚如何用Sqlite數據做到這一點。 任何人都可以幫助我如何實現這個教程的鏈接,或者可以幫助我將數據庫列轉換爲我可以使用的數組。基於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; 
     } 

錯誤日誌 - >

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) 
+0

[從這裏啓發(http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/) – CRUSADER

+0

@CRUSADER感謝評論。但我已經有了我的數據庫設置和工作。我只是不知道如何將某個列轉換爲數組,並在繪製圖形時進一步使用它。 –

+0

你的意思是你想從你的數據庫列返回字符串數組..對嗎? – CRUSADER

回答

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列的數據,但您可以獲取任何列...

希望這有助於..

+0

在你的問題中發佈你的代碼,以便我們可以引導你.. – CRUSADER

+0

@PoojaGaonkar檢查'reportCursor!= null' ..燃料表中是否有任何數據? – CRUSADER

+0

你從reportCursor.getCount()得到的檢查計數值不爲零 – CRUSADER