从AsyncTask的类onPostExecute方法的返回值 [英] Return value from AsyncTask class onPostExecute method

查看:587
本文介绍了从AsyncTask的类onPostExecute方法的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以现在我有 A类包含了一些纺纱厂的值将被填充B级扩展 AsnycTask 从Web服务抓住了微调值。在B类我设法检索值,显示举杯。现在的问题是我如何通过这些微调值回A类?

Ok so now I have Class A that contains some spinners that values will be populated by Class B that extends AsnycTask which grabs the spinner values from a web service. In class B i manage to retrieve the values, showing in a Toast. The problem now is how do I pass those spinner values back to Class A?

我试过

<一个href="http://stackoverflow.com/questions/10005353/can-onpostexcecute-method-in-asynctask-return-values">Can OnPostExcecute方法AsyncTask的返回值?

通过传递 A类 B类和值存储在 A类象下面这样

by passing Class A to Class B and store the value in a public variable of Class A like below

@Override
protected void onPostExecute(String result)
{
  classA.classAvariable = result;
}

不过,每当我尝试读取 classAvariable 我总是一空指针异常。 好像变从未分配的结果。 为便于阅读的目的,我需要单独的 B类而不是使用作为一个内联类。

However whenever I try to read the classAvariable i always get a NullPointer Exception. Seems like the variable was never assigned with the result. For readability purpose I needed to seperate Class B instead of using as an inline class.

任何想法,我的同胞的Java程序员?

Any ideas my fellow Java programmers?

推荐答案

我想你想读A类变量,它被设置前.. 试试使用callbacks..in回调函数传递值,并刷新你的纺纱厂做..

I guess you are trying to read class A variable before it is being set.. Try to do it using callbacks..in the callback function pass the values and refresh your spinners..

您可以创建接口,把它传递给的AsyncTask (在构造函数),然后调用方法 onPostExecute

You can create interface, pass it to AsyncTask (in constructor), and then call method in onPostExecute

例如:

您接口:

public interface OnTaskCompleted{
    void onTaskCompleted(values);
}

您活动:

public YourActivity implements OnTaskCompleted{
    //your Activity
    YourTask  task = new YourTask(this); // here is the initalization code for your asyncTask
}

和你的AsyncTask:

And your AsyncTask:

public YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type
    private OnTaskCompleted listener;

    public YourTask(OnTaskCompleted listener){
        this.listener=listener;
    }

    //required methods

    protected void onPostExecute(Object o){
        //your stuff
        listener.onTaskCompleted(values);
    }
}

这篇关于从AsyncTask的类onPostExecute方法的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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