[英]passing objects from AsyncThread to Fragment Class
public SimpleAsyncTask(JSONObject obj, Activity activity) {
this.obj = obj;
this.activity = activity;
}
@Override
protected String doInBackground(String...params) {
String result = "";
return result;
}
@Override
protected void onPostExecute(String result) {
List<Appointments> lists = new ArrayList<Appointments>();
return lists;
}
@Override
protected void onPreExecute() {
}
I call my AsyncTask Thread from MainUI thread. 我从MainUI线程调用AsyncTask线程。 I have a fragment class which has a listView, how would i pass the
List to my Fragment class.
我有一个带有listView的fragment类,如何将
List to my Fragment class.
传递List to my Fragment class.
ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(),
android.R.layout.simple_list_item_1, listObject);
setListAdapter(adapter);
Just make the lists variable a member of your Fragment class, instead of being scoped within your AsyncTask 只需使列表变量成为Fragment类的成员即可,而不是在AsyncTask中进行作用域限定
something like this: 像这样的东西:
public class MyFragment extends Fragment {
private List<Appointments> lists = new ArrayList<Appointments>();
......
then in your AsyncTask, use it as follows: 然后在您的AsyncTask中,如下使用它:
@Override
protected void onPostExecute(String result) {
// Not instantiating it from scratch, but using the class variable
lists = new ArrayList<Appointments>();
return lists;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.