AndroidPlot:无法在Android应用中显示列表数据的折线图

时间:2014-09-08 18:03:54

标签: android mysql json list androidplot

我想在AndroidPlot折线图中显示一些浮动温度值与时间戳,我正在通过JSON解析从MySQL数据库中检索这些值的几组。温度值接收正常,我试图将它们保存在List中,但是当我绘制值时,设备上的GUI中没有任何内容显示。

以下是该程序的一些细节:我使用AsyncTask在方法doInBackground()中检索临时值,并将绘制图形的代码放在onPostExecute()中。我正在从数据库中检索5个值,但奇怪的是,当我将它们放在List中并尝试将图形映射到像Number[] seriesNumbers = { 1, 2, 3, 4, 5};这样的约5个常量值时,我在LogCat中得到xVal和{的错误{1}}参数必须大小相同。我已经打印出了我从JSON解析器获得的内容,而且确实只有5个值。我认为至少应该在yVal对自己进行绘制时起作用,但在这种情况下图表是空白的。

我的完整活动代码:

arraylist1

该程序使用默认测试功能正常工作,并且折线图显示:

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);
    }
}

当我这样做时它也不会崩溃,但是没有线,只有空白图表GUI:

XYSeries series1 = new SimpleXYSeries(Arrays.asList(seriesNumbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1");

我在这里做错了什么?我怎样才能遍历arraylist1以查看温度值是否实际保存在列表中? SimpleXYSeries()是否甚至首先接受我在这里发送的参数?

感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

假设您的后台线程按预期运行,最可能的问题是您在添加数据时不会重绘绘图。尝试在doInBackground(...)方法中的 arraylist1.add(tempVal)之后添加此内容:

plot.redraw();