我想在 AndroidPlot 折线图中根据时间戳显示一些浮点温度值,并且我正在通过 JSON 解析从 MySQL 数据库检索几组这些值。温度值接收得很好,我试图将它们保存在列表中,但是当我绘制这些值时,设备上的 GUI 中没有显示任何内容。
以下是该程序的一些详细信息:我正在使用 AsyncTask 来检索方法 doInBackground()
中的临时值。 ,并将绘制图形的代码放在 onPostExecute()
中。我正在从数据库中检索 5 个值,但奇怪的是,当我将它们放入列表中并尝试根据像 Number[] seriesNumbers = { 1, 2, 3, 4, 5};
这样的 5 个常量值绘制图表时。我在 LogCat 中收到错误 xVal
和yVal
参数必须具有相同的大小。我已经打印出了从 JSON 解析器获得的内容,果然只有 5 个值。我认为它至少应该在我绘制 arraylist1
时起作用与自身相反,但在这种情况下图表是空白的。
我的完整 Activity 代码:
public class MainActivity extends Activity {
public static XYPlot plot;
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
static List<Number> arraylist1 = new ArrayList<Number>();
private static String url_sensor_data = "http://iotautomationtech.com/android_connect/get_sensor_data.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_deviceS = "devices";
private static final String TAG_PID = "id";
private static final String TAG_VAL = "value";
private static final String TAG_TIME = "timestamp";
Number[] seriesNumbers = { 1, 2, 3, 4, 5 };
JSONParser jsonParser = new JSONParser();
JSONArray devices = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_xy_plot_example);
plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
new LoadSensorData().execute();
plotDraw();
}
class LoadSensorData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading sensor data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_sensor_data, "GET",
params);
Log.d("Sensor data: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
devices = json.getJSONArray(TAG_deviceS);
for (int i = 0; i < devices.length(); i++) {
JSONObject c = devices.getJSONObject(i);
String id = c.getString(TAG_PID);
String value1 = c.getString(TAG_VAL); // <-- temperature value from JSON parser
String time = c.getString(TAG_TIME);
value1.trim(); // <-- trim the string
if (isFloat(value1)) {
float tempVal = Float.parseFloat(value1);
arraylist1.add(tempVal); // <-- add it to arraylist1 if it is valid float
}
Log.d("Check: ", value1.toString()); // <-- returns 5 values in LogCat
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
public static boolean isFloat(String number) {
try {
return (!new Float(number).isNaN());
} catch (NumberFormatException e) {
return false;
}
}
public void plotDraw() {
plot.setDomainLabel("Time");
plot.setRangeLabel("Temp");
plot.setTitle("Temperature Trend");
XYSeries series1 = new SimpleXYSeries(arraylist1, arraylist1, "Series1");
LineAndPointFormatter series1Format = new LineAndPointFormatter();
series1Format.setPointLabelFormatter(new PointLabelFormatter());
series1Format.configure(getApplicationContext(),
R.xml.line_point_formatter_with_plf1);
plot.addSeries(series1, series1Format);
plot.setTicksPerRangeLabel(3);
plot.getGraphWidget().setDomainLabelOrientation(-45);
}
}
程序在默认测试功能下运行良好,并且显示了折线图:
XYSeries series1 = new SimpleXYSeries(Arrays.asList(seriesNumbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1");
当我这样做时,它也不会崩溃,但没有线条,只有空白图表 GUI:
XYSeries series1 = new SimpleXYSeries(arraylist1, arraylist1, "Series1");
我在这里做错了什么?如何遍历 arraylist1 以查看温度值是否确实保存在列表中? SimpleXYSeries() 是否接受我首先在此处发送的参数?
感谢您的帮助!
最佳答案
假设您的后台线程的行为符合预期,最可能的问题是您在添加数据时从不重新绘制绘图。尝试在 doInBackground(...) 方法中的 arraylist1.add(tempVal) 之后添加此内容:
plot.redraw();
关于AndroidPlot:无法在 Android 应用程序中显示列表数据的折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25730405/