我有一个GraphView在添加新数据时不会更新,它会更改Y值以适应新数据,但不会绘制它们,这是代码:

 public class MainActivity extends Activity {
    double data = 1;
    double xgraph = 5;
    GraphViewSeries sensorSeries;
    GraphView graphView;
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        sensorSeries = new GraphViewSeries(new GraphViewData[] {
                new GraphViewData(1, 2.0d),
                new GraphViewData(2, 1.5d),
                new GraphViewData(3, 2.5d),
                new GraphViewData(4, 1.0d)
        });
        graphView = new LineGraphView(this,"TEST");
        graphView.addSeries(sensorSeries);
        LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
        graphView.setViewPort(0, 20);
        layout.addView(graphView);

        refreshButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sensorSeries.appendData(new GraphViewData(xgraph, data), false);
                graphView.redrawAll();
                myLabel.setText(""+data+" "+xgraph);
                xgraph++;
                dataAcx++;
            }
        });
    }
    ...
}

最佳答案

现在,它的价值很高,但不会更改UI,因为您需要在单独的线程中执行此类UI更新操作。您需要将所有代码放在onClick / Thread中的Runnable中。在Thread内创建onClick对象并放置该代码。

请参考以下链接:

http://karanbalkar.com/2014/05/display-graphs-using-graphview-in-android/

08-16 18:51