如何通过不同的对象作为参数传递给AsycTask?对象、不同、参数、AsycTask

2023-09-04 12:32:46 作者:学不会、敷衍

我使用下面的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.

推荐答案

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

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...