将对象从AsyncThread传递到Fragment类

时间:2013-03-31 12:51:17

标签: java android

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() {

}

我从MainUI线程调用我的AsyncTask线程。我有一个片段类,它有一个listView,我将如何通过List to my Fragment class.

ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(),
        android.R.layout.simple_list_item_1, listObject);
setListAdapter(adapter);

1 个答案:

答案 0 :(得分:1)

只需将lists变量作为Fragment类的成员,而不是在AsyncTask中作为范围

类似的东西:

public class MyFragment extends Fragment {

    private List<Appointments> lists = new ArrayList<Appointments>();
    ......

然后在AsyncTask中,按如下方式使用它:

    @Override
    protected void onPostExecute(String result) {
        // Not instantiating it from scratch, but using the class variable
        lists = new ArrayList<Appointments>();
        return lists;
     }