将Integer []传递给AsyncTask - Android

时间:2015-05-17 04:38:30

标签: android android-asynctask integer

我无法将ArrayList传递给扩展AsyncTask的类。 AsyncTask类DownLoaderTask是在DownloaderTaskFragment类中创建的。 DownloaderTaskFragment和DownLoaderTask的代码如下。

此应用程序的MainActivity通过设置Bundle参数将ArrayList传递给片段。在下面的代码中,我正在检索参数,创建一个新的Integer [],mResourceIds来存储传递的值。

我很困惑如何将Interger []传递到DownLoaderTask以传递mResourceIds中的所有值。将参数传递给DownLoaderTask后,downloadTweets()函数将用于检索所需的数据。 downloadTweets()返回一个String []。有人可以引导我完成将数据从Integer []传递到AsyncTask的过程吗?

我是Android编程的新手,并且一直在努力解决AsyncTasks如何处理参数问题。非常感谢您的帮助。谢谢你的进步!

public class DownloaderTaskFragment extends Fragment {

    private DownloadFinishedListener mCallback;
    private Context mContext;
    static final String TAG_FRIEND_RES_IDS = "friends";

    @SuppressWarnings ("unused")
    private static final String TAG = "Lab-Threads";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Preserve across reconfigurations
        setRetainInstance(true);

        // TODO: Create new DownloaderTask that "downloads" data
        DownLoaderTask downLoaderTask = new DownLoaderTask();


        // TODO: Retrieve arguments from DownloaderTaskFragment
        // Prepare them for use with DownloaderTask. 
        Bundle args = getArguments();

        //.getIntegerArrayList returns the value associated with the given key
        ArrayList arrayList = args.getIntegerArrayList(TAG_FRIEND_RES_IDS);

        //Create integer array for size of arrayList passed
        Integer[] mResourceIds = new Integer[arrayList.size()];

        //Initialize mResourceIds with values from arrayList
        //arrayList must be cast to get Integer[], otherwise Object[] is returned
        mResourceIds = (Integer[]) arrayList.toArray(mResourceIds);


        // TODO: Start the DownloaderTask 
        downLoaderTask.execute(mResourceIds);

    }

    // Assign current hosting Activity to mCallback
    // Store application context for use by downloadTweets()
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mContext = activity.getApplicationContext(); 

        // Make sure that the hosting activity has implemented
        // the correct callback interface.
        try {
            mCallback = (DownloadFinishedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement DownloadFinishedListener");
        }
    }

    // Null out mCallback
    @Override
    public void onDetach() {
        super.onDetach();
        mCallback = null;
    }


    // TODO: Implement an AsyncTask subclass called DownLoaderTask.
    // This class must use the downloadTweets method (currently commented
    // out). Ultimately, it must also pass newly available data back to
    // the hosting Activity using the DownloadFinishedListener interface.

    //doInBackground
    public class DownLoaderTask extends AsyncTask<Integer, Void, String[]> {

        protected String[] doInBackground(Integer... params) {
            //Create a String[] for feed response, should be length of params passed
            //Helper method downloadTweets will return a String Array

            String[] feeds = downloadTweets(params);
            Log.v("doInBackground", feeds.toString());
            return feeds;

        }

        //Pass data back to hosting Activity
        protected void onPostExecute(String[] feeds) {

            mCallback.notifyDataRefreshed(feeds);
        }

    }

1 个答案:

答案 0 :(得分:1)

将AsyncTask的参数更改为<Integer[], Void, String[]>

public class DownLoaderTask extends AsyncTask<Integer[], Void, String[]> {

    ...

    protected String[] doInBackground(Integer[]... params) {
        ...
        Integer[] resourceIds = params[0];
        ...
    } 

    ...

}