我无法从私有的Oncreate方法获取四个变量的值到公共的Intent方法。
下面的类创建一个饼图。现在我正在尝试使用我的SQlite数据库中的数据填充此数组。 int[] values = {dimensionPhysical, dimensionMental, dimensionSocial, dimensionSpirituality};
数据库在oncreate方法中打开,我在下面的getcount查询中运行4次。
public int getCount(String rowId) {
Cursor C = db.rawQuery("select count {*} from mainTable where " + KEY_DIMENSION + " = " + rowId, null);
return C.getCount();
}
不知怎的,我无法从私有onCreate方法获取四个变量的值到公共Intent方法。饼图基于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;
}
}
我希望有人知道如何解决这个问题。
答案 0 :(得分:-1)
尝试用单引号包装KEY_DIMENSION(我猜它是一个字符串):
Cursor C = db.rawQuery("select count {*} from mainTable where " + KEY_DIMENSION + " = '" + rowId + "'", null);
在任何情况下,学习使用logcat甚至调试器。