如何让不同的方式AsyncTask的UI更新 [英] How to let Asynctask update UI in different ways

查看:114
本文介绍了如何让不同的方式AsyncTask的UI更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在用Asyntask运行到经常出现问题。联系UI线程的方法是调用publishProgress()及此方法接受的只有一个类型的参数的数组。

This is a problem that I keep running into often with using Asyntask. The way to contact the UI thread is to call publishProgress() & this method accepts an array of only one TYPE of parameter.

在后台做复杂的计算正在运行的线程可能需要使用不同类型在不同的点来更新UI 的对象。

A thread running in the background doing complicated computations might need to update the UI at different points using different types of object.

让我说明一个例子:

...do some processing...
// Send UI thread the integer values of the width & height of the image 
...do some more processing...
// Send UI thread a String with custom message.
...do some more processing...
// Send UI thread an instance of MyObject so it can extract & display certain values 
...do some cleanup job & finish...

然而,onProgressUpdate()只接受一种类型的阵列

However, onProgressUpdate() accepts an array of only one type.

所以,做我作出这样一个包罗万象的对象的类型?我怎么知道怎么垂头丧气它,因为这种方法可以从1号线,2或3个被称为所以这时候了?

So do I make that an all encompassing Object type? How do I know how to downcast it since this method can be called from line 1, 2 or 3 so which time is it?

毫无疑问,我们必须要做到这一点的好办法?

Surely there must be a good way to achieve this?

编辑:我真的很想看看,如果在Android中是可能的,将是界定某种方式的 publishProgress1(用户定义args1) publishProgress2(用户自定义args2) publishProgress3(用户定义args3) ...

What I'd really love to see, if it were possible in Android, would be some way of defining publishProgress1(user-defined args1), publishProgress2(user-defined args2), publishProgress3(user-defined args3) ...

推荐答案

在你的第三个情况......

In your 3rd case...

//发送UI线程MyObject的实例

// Send UI thread an instance of MyObject

...有说,虽然这取决于你从你的插图是什么意思,你这样做,尤其onPostExecute()的参数。

...there's an argument to say you would do this in onPostExecute() although that depends on what you meant from your illustration.

您可以很容易地做你在传递一个包罗万象的对象建议。对象可以具有各种领域(整数,字符串对象)加上一个行动采取一项描述这些字段是有效的,并需要处理

You could easily do as you suggest in passing an all encompassing object. The object could have various fields (integer, string, object) plus an 'action' to take describing which of those fields are valid and need to be processed.

您可以简单地传递一个int枚举,如 PROCESS_INT_WIDTH_AND_HEIGHT PROCESS_STRING_MESSAGE PROCESS_OBJECT 等有什么能阻止你这样做......

You could simply pass an int enum such as PROCESS_INT_WIDTH_AND_HEIGHT, PROCESS_STRING_MESSAGE, PROCESS_OBJECT etc. There's nothing to stop you doing this...

private static class MyAsyncTask extends AsyncTask<String, int, Void> {
    private int width;
    private int height;
    private String customMessage;
    private MyObject myObject;

    @Override
    protected Void doInBackground(String... params) {

        width = 10;
        height = 10;
        publishProgress(PROCESS_INT_WIDTH_AND_HEIGHT);
    }

    protected void onProgressUpdate(int... progress) {
        if (progress == PROCESS_INT_WIDTH_AND_HEIGHT)
            // Process width and height
    }
}

在换言之,onProgressUpdate()方法简单地响应一个命令,并相应地处理所述有关私人领域

In other words, the onProgressUpdate() method simply responds to a 'command' and processes the relevant private fields accordingly.

这篇关于如何让不同的方式AsyncTask的UI更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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