Search code examples
androidsqliteandroid-sqliteachartenginelinechart

How do I fetch the values from the Android SQLite and store into array?


I want to make graph for line chart. so I refer achartEngine , here is my code :

int[] x = {1, 2, 3, 4, 5};
int[] y = {24, 33, 15, 20, 55};
TimeSeries serial = new TimeSeries("Line 1");
for (int i = 0 ; i < x.length ; i++) {
     serial.add(x[i], y[i]);
}

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(serial);

no question so far , but I don't know how to replace array with int.

for example , I use sqlite like :

SQLiteDatabase db = dbhelper.getReadableDatabase();
String[] columns = {KEY_ID, TEMPER};  
Cursor cursor = db.query(TABLE_NAME, null, null);
int id = cursor.getColumnIndex(KEY_ID);
int ect = cursor.getColumnIndex(TEMPER);

I am in this situation , any suggestions for me?


Solution

  • You don't need the array. Wherever you are using it, replace it with values read from the cursor:

    int id = cursor.getColumnIndexOrThrow(KEY_ID);
    int ect = cursor.getColumnIndexOrThrow(TEMPER);
    while (cursor.moveToNext()) {
        serial.add(cursor.getInt(id), cursor.getInt(ect));
    }