GarphView无法正常工作,空白活动

时间:2014-09-26 06:36:43

标签: java android android-activity android-graphview

我动态地在我的Main活动类中实现了GraphView。 没有结果显示在其中。 启动时活动仍为空白。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //GraphView
    int num = 150;
    GraphViewData[] data = new GraphViewData[num];
    double v=0;
    for (int i=0; i<num; i++) {
      v += 0.2;
      data[i] = new GraphViewData(i, Math.sin(v));
    }
    GraphView graphView = new LineGraphView(this, "GraphViewDemo");
    // add data
    GraphViewSeries graphViewSeries=new GraphViewSeries(data);
    graphView.addSeries(graphViewSeries);
    // set view port, start=2, size=40
    graphView.setViewPort(2, 40);
    graphView.setScrollable(true);
    // optional - activate scaling / zooming
    graphView.setScalable(true);
    Log.d("2", "closed");
    LinearLayout layout = (LinearLayout)  findViewById(R.id.layout);
    layout.addView(graphView);
    //GraphhView
}

我会想念一些事情,但我什么也得不到。

1 个答案:

答案 0 :(得分:0)

您需要在代码中创建一个实现GraphViewDataInterface的GraphViewData类,如下例所示:

public class GraphViewData implements GraphViewDataInterface {
private double x,y;

    public GraphViewData(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public double getX() {
        return this.x;
    }

    @Override
    public double getY() {
        return this.y;
    }   
}