在AsyncTask中,如何将数据从doInBackground()传递到主UI& quot; instantly& quot;.而不是在每个循环之后?(BufferedReader) [英] In AsyncTask How to pass data from doInBackground() to main UI "instantly" and not after each loop? (BufferedReader)

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

问题描述

我确实更改了代码,使得更新在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次迭代完成后,我只会获得一次AT的结果.真奇怪我希望每次迭代后都输出结果.如何实现这一目标.目前;

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("\n");
            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线程中运行.

因此,您应该能够在循环内使用此方法与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("\n");
}

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

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

这篇关于在AsyncTask中,如何将数据从doInBackground()传递到主UI&amp; quot; instantly&amp; quot;.而不是在每个循环之后?(BufferedReader)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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