随着时间的推移从ArrayList更新图表吗?

问题描述:

我有一个ArrayList值,我想遍历ArrayList.对于每个新值,我想用该值更新图表,然后等待一段时间,然后对下一个值执行相同的操作.

I have an ArrayList of values, and I would like to iterate through the ArrayList. For every new value, I would like to update the chart with that value, and then wait a set amount of time before doing the same thing to the next value.

此刻,我的日志显示所有值都被迭代.但是,在我的测试设备上,图表直到最后才更新.到那时,所有的值都被立即加载,因此就没有想要的幻灯片显示"效果.

At the moment, my log says that all of the values are being iterated over. However, on my testing device, the chart does not update until the very end; at that point, all of the values are loaded at once, so there is no desired "slideshow" effect.

当我要开始播放ArrayList中的值时,此方法称为:

When I want to start playing back the values in my ArrayList, this method is called:

public void playback(){
    if(ret != null) {
        for (int x = 0; x < ret.size(); x++) {
            addEntry(ret.get(x));
            try {
                Thread.sleep(100);
            } catch (Exception e){
                //Do nothing
            }
        }
    } else {
        Log.d(LOG_TAG, "ret was null.");
    }
}

我该怎么做才能使这些值一个接一个地显示在图表上,并且每个值之间有一定的时间间隔?

What can I do so that the values are displayed on my chart, one after another, with a certain amount of time between each value?

以下是我在Shadab Ansari的帮助下最终实现的解决方案:

Here was the solution I ended up implementing with help from Shadab Ansari:

public void playback(){
    if(ret != null) {
        addEntry(0);
    } else {
        Log.d(LOG_TAG, "ret was null.");
    }
}



private void addEntry(int index) {
    final int in = index;

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            yVals1.get(0).setVal(ret.get(in).intValue());
            RadarDataSet set1 = new RadarDataSet(yVals1, "Set 1");
            // And other UI stuff

            // Recursive call!
            if(in < ret.size() - 1){
                addEntry(in + 1);
            }
        }
    }, 100);

}

如果不清楚,ret是一个全局变量,包含要插入的数组. yVals1是用于填充雷达图的ArrayList of Entries.

In case it was not clear, ret was a global variable that contained the arrays that I was going to be inserting. yVals1 was an ArrayList of Entries to populate the radar chart.

最终结果是,在此示例代码中,图表每隔100毫秒用ArrayList中的下一个值更新一次.在这段时间里,我仍然可以放大/缩小图表并旋转它.

The end result is that, in this example code, the chart is updated with the next value in my ArrayList every 100 milliseconds. During this time I can still zoom in/out of the chart and rotate it with no problems.

如果您的addEntry()执行UI操作,那么让我解释一下您的问题-

If your addEntry() performs a UI operation then let me explain your problem -

说明-

Android是基于事件的系统.设备上发生了某些事情(触摸了屏幕,按下了按键等),Android引发了一个事件.应用程序会收到事件通知,当事件发生时需要响应它,通常会运行您编写的代码.您的应用在Android操作系统(OS)的控制下循环运行其代码.此代码循环称为App的执行线程.只有一个线程,它负责运行应用程序代码和更新显示.

Android is an event based system. Something happens on the device (the screen is touched, a key is pressed, etc.) and Android raises an event. An App is notified of an event and when one occurs that it needs to respond to it does so, often running the code that you have written. Your App runs its code in a loop under the control of the Android Operating Systems (OS). This code loop is referred to as the App's thread of execution. There is only one thread and it is responsible for both running the App code and updating the display.

因此,UI更新不会立即发生,并且每次循环运行时,都会使UI线程休眠100 ms.并且当Android尝试更新UI时,您使线程处于睡眠状态,这意味着在此时间段内UI线程将不执行任何操作.直到循环完成为止.循环结束后,将执行最终事件,您将看到通过addEntry()调用更新的UI,并传递了最新的值.

So the UI update does not happen immediately and your making the UI thread sleep for 100 ms every time the loop runs. And when Android tries to update the UI, you make the thread sleep which means during this time period UI thread will not do anything. And this happens till your loop finishes. After your loop ends, the final event gets executed and you will see your UI updated by the call of addEntry() with the latest value passed.

解决方案-

Solution -

您可以使用postDelayed()-

new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                           //Perform your task and it will be executed after 100 ms
                        }
                    },100);