Android 图形视图更新

Android Graph View Update

作为一个 Java 和 Android 初学者,我发现大多数文档仍然很难理解,但我认为这可以由知道他们在做什么的人轻松解决:

我正在使用 Graph View 的 Android Studio 来构建一个简单的图表。到目前为止效果很好,但我想要实现的是通过按下按钮 .

来设置我的数据数组的最后一个变量 +1 或 -1

所以我在应用启动时生成了一个固定的数据集:

private LineGraphSeries<DataPoint> mSeries1 = new LineGraphSeries<DataPoint>(new DataPoint[] {
        new DataPoint(1, 1),
        new DataPoint(2, 5),
        new DataPoint(3, 3),
        new DataPoint(4, 2),
        new DataPoint(5, mCigCount)
});

其中 mCigCount 是我所说的变量。它通过按下按钮获得 +1 或 -1,这已经可以正常工作了。

问题是,我如何更新我的图表,以便在按下按钮后正确呈现 mCigCount 的新值?

This tutorial does it in a complicated way with some random numbers and updates every second, which is still way too confusing for me. It also mentions the methods resetData and appendData 我没能正确使用,因为 我不明白我必须在里面放什么 (...):

mSeries1.resetData(...);
mSeries1.appendData(...);

如何找到我的最后一个数据点并更新其中的 mCigCount?

好吧,我找到了一个解决方法,虽然这在我看来不是一个好的解决方案,所以请随时使用 resetData 和 appendData 纠正我!

我基本上擦除系列中的所有数据,然后全新写入。这种方法的问题是,视图在每次更新时都在闪烁,因为它被删除并重新创建:

GraphView graph = (GraphView) findViewById(R.id.graph);
    graph.removeAllSeries();
    LineGraphSeries<DataPoint> mSeries1 = new LineGraphSeries<DataPoint>(new DataPoint[] {
            new DataPoint(1, 1),
            new DataPoint(2, 5),
            new DataPoint(3, 3),
            new DataPoint(4, 2),
            new DataPoint(5, mCigCount)
    });
    LineGraphSeries<DataPoint> series = mSeries1;
    graph.addSeries(series);