gpt4 book ai didi

android - 将 Integer[] 传递给 AsyncTask - Android

转载 作者:行者123 更新时间:2023-11-30 02:09:41 26 4
gpt4 key购买 nike

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

此应用程序的 MainActivity 通过设置 Bundle 参数将 ArrayList 传递给 fragment 。在下面的代码中,我正在检索参数,创建一个新的 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);
}

}

最佳答案

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

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

...

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

...

}

关于android - 将 Integer[] 传递给 AsyncTask - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30283426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com