将数据从数据库加载到Datapoint []

时间:2016-03-19 17:39:15

标签: android android-graphview

我正在尝试将数据导入LineGraphSeries,但我遇到了问题。 我想创建一个图形(不是实时修改)与插入SQLite数据库的数据,我写了以下代码来做到这一点。 因为我在自己的最后一年项目中自学java,所以我对android的经验很少,如果有人能提供帮助,我将感激不尽。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mediasdiab);


    mediaglicemia = (TextView) findViewById(R.id.textmediaglicemia);
    mediainsulina = (TextView) findViewById(R.id.textmediainsulina);
    mediaporcoes = (TextView) findViewById(R.id.textmediaporcoes);
    maxg = (TextView) findViewById(R.id.txtglicmax);
    maxi = (TextView) findViewById(R.id.txtinsmax);
    maxp = (TextView) findViewById(R.id.txtpormax);
    ming = (TextView) findViewById(R.id.txtglicmin);
    mini = (TextView) findViewById(R.id.txtinsmin);
    minp = (TextView) findViewById(R.id.txtpormin);

    try {
        database = new Database(this);
        conn = database.getWritableDatabase();
        operacoes = new operacoes(conn);

    }catch (SQLException ex)
    {
        Toast.makeText(getApplicationContext(), "Base de dados não acessivel. Erro:" + ex.getMessage(), Toast.LENGTH_LONG).show();
    }
    graph = (GraphView) findViewById(R.id.graficodiab);
    listardados();
    gerardatapoints();
    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(values);
    graph.addSeries(series);

}

    //The error happens here
private void gerardatapoints(){
    Cursor cursor = operacoes.todoaasfilas();
    values = new DataPoint[idalto];
    for (z=0; i<idalto; z++) {
        cursor.moveToPosition(z);
        diablinhagli = cursor.getString(cursor.getColumnIndex("GLICEMIA"));
        graphgic = cursor.getInt(0);
        values[z]= new DataPoint(z, graphgic);
    }
    seriesglicemia = new LineGraphSeries<DataPoint>(values);
}

1 个答案:

答案 0 :(得分:0)

1.设置DataPoint阵列的大小 2.使用整数参数创建数据点 3.将数据点添加到DataPoint阵列 4.Initialise LineGraphSeries与数组。

    private void gerardatapoints() {
    Cursor cursor = operacoes.todoaasfilas();
    values = new DataPoint[idalto];
    for (z=0; i<idalto; z++) {
    cursor.moveToPosition(z);
    diablinhagli = cursor.getString(cursor.getColumnIndex("GLICEMIA"));
    graphgic = cursor.getInt(0);
        //Assuming graphgic is of integer type
       DataPoint v = new DataPoint(z, graphgic);
        values[i] = v;
    }

}