将 Integer[] 传递给 AsyncTask - Android

Pass Integer[] to AsyncTask - Android

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

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

我对如何将 Interger[] 传递到 DownLoaderTask 以传递 mResourceIds 中的所有值感到困惑。将参数传递给 DownLoaderTask 后,将使用 downloadTweets() 函数检索所需的数据。 downloadTweets() returns 一个 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);
        }

    }

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

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

    ...

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

    ...

}