简体   繁体   中英

Using variable created in private Oncreate() in public Intent

I cannot get the values of the four variables from the private Oncreate method to the public Intent method.

Below class creates a Pie Chart. Now i'm trying to fill this array with data from my SQlite database. int[] values = {dimensionPhysical, dimensionMental, dimensionSocial, dimensionSpirituality};

The database is openend in the oncreate method and I run 4 times the getcount query below.

 public int getCount(String rowId) {
 Cursor C =  db.rawQuery("select count {*} from mainTable where " + KEY_DIMENSION + " = " + rowId, null);
return C.getCount();
}

Somehow I cannot get the values of the four variables from the private onCreate method to the public Intent method. The piechart is based on the intitial values of 1.

public class Balance extends Activity {
static DBAdapter myDb;
public int dimensionPhysical= 1;
public int dimensionMental= 1;
public int dimensionSocial= 1;
public int dimensionSpirituality= 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_balance);
    openDB();
    dimensionPhysical = myDb.getCount("Physical");
    dimensionMental = myDb.getCount("Mental");
    dimensionSocial = myDb.getCount("Social");
    dimensionSpirituality = myDb.getCount("Spiritual");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    closeDB();
}

private void openDB() {
    myDb = new DBAdapter(this);
    myDb.open();


}

private void closeDB() {
    myDb.close();
}

/*
 * UI Button Callbacks
 */

public void onClick_ClearAll(View v) {
    myDb.deleteAll();
}



public Intent getIntent(Context context) {




    int[] values = {dimensionPhysical, dimensionMental, dimensionSocial, dimensionSpirituality};
    CategorySeries series = new CategorySeries("Pie Graph");
    int k = 0;
    for (int value : values) {
        series.add("Section " + ++k, value);
    }

    int[] colors = new int[]{Color.rgb(144, 238, 144), Color.rgb(173,216,230), Color.rgb(255,204,153), Color.rgb(240,204,153)};

    DefaultRenderer renderer = new DefaultRenderer();
    for (int color : colors) {
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(color);
        renderer.addSeriesRenderer(r);
    }
    renderer.setChartTitle("Pie Chart Demo");
    renderer.setChartTitleTextSize(7);
    renderer.setZoomButtonsVisible(true);

    Intent intent = ChartFactory.getPieChartIntent(context, series, renderer, "Pie");
    return intent;
}

}

I hope someone here knows how to fix this problem.

Try wrapping KEY_DIMENSION in single quotes (I guess it's a string):

Cursor C =  db.rawQuery("select count {*} from mainTable where " + KEY_DIMENSION + " = '" + rowId + "'", null);

In any case, learn to use logcat and even debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM