在 AsyncTask 中如何“立即"将数据从 doInBackground() 传递到主 UI而不是在每个循环之后?(缓冲阅读器) [英] In AsyncTask How to pass data from doInBackground() to main UI "instantly" and not after each loop? (BufferedReader)

查看:17
本文介绍了在 AsyncTask 中如何“立即"将数据从 doInBackground() 传递到主 UI而不是在每个循环之后?(缓冲阅读器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑 1:

我确实更改了代码,以便在 while 循环内完成更新.但是,没有变化.在下面的代码中显示了这一点.此外,还删除了 onProgressUpdate() 上的其他不相关行.如下代码所示.

I did change the code such that the update was done within the while loop. HOWEVER, NO CHANGE. SHOWN THIS IN CODE BELOW. ALSO, removed other irrelevant lines on onProgressUpdate(). SHOWN IN CODE BELOW.

请像教业余爱好者一样回答.我目前正在将数据从 doInBackground() 传递到主 UI.有用.但是,它非常缓慢.我想显示即时"结果,而不是在循环完成之后.这就是代码的编写方式,但是,在性能方面,如果循环运行 100 次,我只会在 100 次迭代完成后得到一次结果.这很奇怪.我希望每次迭代后输出结果.如何实现这一点.目前;

Please answer like you would teach an amateur. I am passing data from doInBackground() to the main UI currently. It works. However, it is very slow. I want to display "instant" results and not after the loop completes. That's how the code is written, however, performance wise, if the loop runs for 100 times, I only get the results AT once after 100 iterations are complete. This is weird. I want results output after every iteration. How to achieve this. Currently;

    protected Void doInBackground(Void... params) 
        {

            try {
    //bunch of code
for(int k=x1[3];k<=x2[3];k++)
            {
//Process process = execute it;
    reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    int j;
                    char[] buffer = new char[4096];
                    StringBuffer output = new StringBuffer();
                    while ((j = reader.read(buffer)) > 0)
                    {
                    output.append(buffer, 0, j); 
                    }
                    str = output.toString();
                    Log.d("1:STR VALUE", str);
                    publishProgress(str);
                    reader.close();  

    } //FOR loop ends
                 }}
    protected void onProgressUpdate(String... values) {
            results1.append(str);
            results1.append("
");
            super.onProgressUpdate(values);
        }

如何修改 StringBuffer 输出,以便在阅读器获取数据后立即获得结果?谢谢

How to modify the StringBuffer output such that I get instant results after the reader gets the data? Thanks

推荐答案

onProgressUpdate 在 UI 线程中运行.

onProgressUpdate runs in the UI thread.

因此,您应该能够在循环内使用此方法与 UI 进行交互.但是,请尝试使用该方法的给定参数:

So you should be able to interact with the UI with this method, inside your loop. However, try to use the given parameter of the method :

protected void onProgressUpdate(String... values)
{
    results1.append(values[0]);
    results1.append("
");
}

调用 super.onProgressUpdate(values); 没有用,因为默认实现是空的.

Calling super.onProgressUpdate(values); is useless as the default implementation is empty.

这篇关于在 AsyncTask 中如何“立即"将数据从 doInBackground() 传递到主 UI而不是在每个循环之后?(缓冲阅读器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆