gpt4 book ai didi

android - AsyncTask 返回 List 到 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:59 24 4
gpt4 key购买 nike

如何将在 AsyncTask 中生成的列表返回给 Activity?

我的 LoadStringsAsync 类:

public class LoadStringsAsync extends AsyncTask<Void, Void, List<String> > {

List<String> str;

@Override
protected void onPreExecute() {
super.onPreExecute();
...
}

@Override
protected List<String> doInBackground(Void... arg0) {
...
get content from the internet
and
fill the list
...
}

@Override
protected void onPostExecute(List<String> str) {
super.onPostExecute(events);
...

}
}

并且我需要将列表返回到我的 Activity 中才能使用它。 (不只是在 ListView 中显示它:P)

有什么建议如何做到这一点? :-)

到目前为止谢谢!

最佳答案

您的 Activity :

public class YourActivity extends Activity {
private List<String> list = new ArrayList<String>();

public void onCreate(Bundle state) {
//...
}

public void setList(List<String> list) {
this.list = list;
}

private void fireYourAsyncTask() {
new LoadStringsAsync(this).execute();
}

}

异步任务:

public class LoadStringsAsync extends AsyncTask<Void, Void, List<String>> {

List<String> str;
private YourAcitivity activity;

public LoadStringsAsync(YourAcitivity activity) {
this.activity = activity;
}

@Override
protected List<String> doInBackground(Void... arg0) {
}

@Override
protected void onPostExecute(List<String> str) {
super.onPostExecute(events);
activity.setList(str);
}
}

关于android - AsyncTask 返回 List 到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17596188/

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