我不确定我是否在概念上遗漏了一些东西,或者我的一种方法是否有错误,但我已经调试了这个活动很长一段时间,我必须忽略一些巨大的东西。在我的 onCreate 结束时,我实例化一个 ASyncTask 以从 GET 填充数据的 ArrayList,然后在 onPostExecute 中我用新数据重置 GraphView 数据。我的 onCreate 如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cholestoral);
graph = (GraphView) findViewById(R.id.graph);
//dummy data for now, TODO: adjust dates to fit in graphical format
HDL_series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 2),
new DataPoint(1, 1),
new DataPoint(2, 5),
new DataPoint(3, 3),
new DataPoint(4, 6),
});
HDL_series.setColor(Color.RED);
HDL_series.setTitle("HDL");
LDL_series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 7),
new DataPoint(1, 8),
new DataPoint(2, 5),
new DataPoint(3, 8),
new DataPoint(4, 7),
});
LDL_series.setColor(Color.CYAN);
LDL_series.setTitle("LDL");
graph.addSeries(HDL_series);
graph.addSeries(LDL_series);
//empty arraylist on page load... to be filled in ASyncTask below
new ListCholestoralAPI().execute("http://m-health.cse.nd.edu:8000/phrService-0.0.1-SNAPSHOT/chol/chol/");
doInBackground 正确填充了我的信息列表,并将其发送到 onPostExecute ,该部分必须是使应用程序不断崩溃的部分。我的 onPostExecute 只是在我的活动中调用了一个名为 initializeGraph() 的函数。
public void initializeGraph() {
//TODO: initialize graph by setting labels, axes, etc...
HDL_series.resetData(generateDataPoints());
}
public DataPoint[] generateDataPoints() {
dataPointArray = null;
dataPointArray = new DataPoint[30];
for (int i = 0; i < cholesterolInformationList.size(); i++) {
double hdl = cholesterolInformationList.get(i).getHdl();
dataPointArray[i] = new DataPoint(i, hdl);
}
//dataPointArray = sortDataPointArray(dataPointArray);
return dataPointArray;
}
注意:如果这里有任何概念上的缺陷,请告诉我!我在一项相当简单的任务上花费了大量时间。