Kevin
Kevin

Reputation: 23634

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. I have a fragment class which has a listView, how would i pass the List to my Fragment class.

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

Upvotes: 0

Views: 36

Answers (1)

Booger
Booger

Reputation: 18725

Just make the lists variable a member of your Fragment class, instead of being scoped within your AsyncTask

something like this:

public class MyFragment extends Fragment {

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

then in your AsyncTask, use it as follows:

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

Upvotes: 1

Related Questions