如何通过不同的对象作为参数传递给AsycTask? [英] How to pass different objects as a parameter to AsycTask?

查看:136
本文介绍了如何通过不同的对象作为参数传递给AsycTask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code创建一个的AsyncTask

I am using following code to create an AsyncTask.

public  class SaveFileToExternalStorage extends AsyncTask<File, Void, Boolean>{

    protected Boolean doInBackground(File... file) {

        DalCategories c= new  DalCategories();
        boolean result  = c.saveObject(customlistobject,file[0]);

        return result;    
    }


    protected void onProgressUpdate() {       
        //setProgressPercent(progress[0]); 

    } 

    protected void onPostExecute(boolean result) {     
        //showDialog("Downloaded " + result + " bytes");  

    }

}

现在我想通过这两个参数 customlistobject 文件与无效的进步和布尔返回类型。

Now I want to pass it two parameters customlistobject and File objects with void progress and boolean return type.

我不知道如何传递 customlistobject 我的的AsyncTask 随着文件对象。

I don't know how to pass that customlistobject to my AsyncTask along with the File object.

推荐答案

一个并不完美,但努力解决方案是使用对象作为参数。<​​/ P>

A not perfect but working solution is to use Object as parameter.

public  class SaveFileToExternalStorage extends AsyncTask<Object, Void, Boolean>{
    protected Boolean doInBackground(Object... param) {
        File file = (File) param[0];
        List list = (CustomList) param[1];
        return result;    
    }

    protected void onProgressUpdate() 
    {       
        //setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(boolean result) 
    {     
        //showDialog("Downloaded " + result + " bytes");  
    }
}

这code是只是一个样本,你应该确保在索引0和1的对象是一个真正的文件或CustomList使用的instanceof

This code is just a sample, you should ensure that the objects at index 0 and 1 are really a File or your CustomList by using instanceof.

屋大维Damiean也写的好方法...

Octavian Damiean has also written a good method...

这篇关于如何通过不同的对象作为参数传递给AsycTask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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