0

I have an implementation of a Fragment class.The class is supposed to return a view of a graph plotted dynamically using data from a remote server.Everything is working ok.But now i want to implement an AsyncTask class to increase responsiveness of the app.Problem is how do i return an ArrayList to the calling class.I can't seem to locate a good example from the internet. Here is my method which creates and returns the view:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.newfragment, container, false);
    sharename = getArguments().getString(EXTRA_SHARENAME);
    // performance = getPerformance(sharename);
    // new GraphSharePerformance().execute();

    new GraphSharePerformance() {
        @Override
        protected void onPostExecute(ArrayList<SharePerformance> param) {
            super.onPostExecute(param);
            ArrayList<SharePerformance> results = param;
            performance = param;
            // USE THE RESULT here
        }
    }.execute();

    LinearLayout layout = (LinearLayout) v.findViewById(R.id.chart);
    layout.addView(displayLineChart(performance));
    return v;
}

And this is my AsyncTask class:

protected class GraphSharePerformance extends
            AsyncTask<GraphFragment, Void, ArrayList<SharePerformance>> {
        ArrayList<SharePerformance> shareperformance = new ArrayList<SharePerformance>();

        protected void onPreExecute() {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("loading");
            progressDialog.show();
            super.onPreExecute();
        }

        @Override
        protected ArrayList<SharePerformance> doInBackground(
                GraphFragment... params) {
            shareperformance = getPerformance(sharename);
            return shareperformance;
        }

        protected void onPostExecute(ArrayList<SharePerformance> param) {
            progressDialog.dismiss();

        }

    }

Now how do i get the returned ArrayList?I have tried using a global variable to hold the arraylist but this doesn't work even though it has always worked in other classes.The graph does not plot any value.I will appreciate help on this.Thank you.

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
mungaih pk
  • 1,809
  • 8
  • 31
  • 57
  • use interface http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask – Raghunandan Oct 28 '13 at 11:53
  • Can you not just put the logic you want to apply on the list inside onPostExecute? – cYrixmorten Oct 28 '13 at 11:53
  • Use [Listener](http://stackoverflow.com/questions/9447646/how-do-i-send-data-back-from-onpostexecute-in-an-asynctask), and try [this](https://www.google.co.in/search?q=listener+in+onpostexecute&oq=listener+in+onpostexecute&aqs=chrome..69i57.8394j0j8&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8) too – Gunaseelan Oct 28 '13 at 11:54
  • I usually implements the delegate pattern – Blackbelt Oct 28 '13 at 11:56
  • @cYrixmorten no i cant do that because if i do i cannot.It is not possible and viable for my case. – mungaih pk Oct 28 '13 at 12:01

5 Answers5

0

try this

protected class GraphSharePerformance extends
    AsyncTask<GraphFragment, Void, GraphFragment> {
ArrayList<SharePerformance> shareperformance = new ArrayList<SharePerformance>();

protected void onPreExecute() {
    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("loading");
    progressDialog.show();
    super.onPreExecute();
}

@Override
protected GraphFragment doInBackground(GraphFragment... params) {
    shareperformance = getPerformance(sharename);
    //return your object here. like below.
    //create object
    GraphFragment f=new GraphFragment();
    //some processing...
    //return object 
    return f;
}

protected void onPostExecute(GraphFragment param) {
    progressDialog.dismiss();
}

}
Rajj
  • 101
  • 7
0

Here is ONE way:

protected class GraphSharePerformance extends
        AsyncTask<GraphFragment, Void, ArrayList<SharePerformance>> {

    protected void onPreExecute() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("loading");
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected ArrayList<SharePerformance> doInBackground(GraphFragment... params) {
        return getPerformance(sharename);
    }

    protected void onPostExecute(ArrayList<SharePerformance> param) {
        progressDialog.dismiss();
    }

}

Finally, when you use your AsyncTask:

new GraphSharePerformance() {
   @Override
   protected void onPostExecute(ArrayList<SharePerformance> param) {
      super.onPostExecute(param);
      ArrayList<SharePerformance> results = param;
      //USE THE RESULT here
   }
}.execute();
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • i have tried your implementation but i have the same problem,the graph is not plotted.I think problem is asigning the returned ArrayList to a global variable named performance.Look at my edit. – mungaih pk Oct 28 '13 at 12:15
  • 1
    Add this line `layout.addView(displayLineChart(results));` in the place of `//USE THE RESULT here`. This means that `layout` should be a global variable. – Sherif elKhatib Oct 28 '13 at 12:23
0

Use an Application class to store values all over your app. Then you can retrieve it everywhere.

Example:

You have to declare this class as follows:

Manifest:

    <application
    android:name="MyApplication"
    android:icon="@drawable/graph"
    android:label="@string/app_name"
    android:theme="@style/Theme.Mytheme" >

.....

 </application>

MyApplication class:

public class MyApplication extends Application{

private ArrayList<SharePerformance> arrayList;

public void setArrayPerf(ArrayList<SharePerformance> ap){
arrayList = ap;
}

public ArrayList<SharePerformance> getArrayPerf(){

 return arrayList;
}
}

In your Activity class:

 MyApplication app = (MyApplication) getApplicationContext();

 app.setArrayPref(sharePerformance);

 ArrayList<SharePerformance> arrayList = app.getArrayPerf();
silvia_aut
  • 1,481
  • 6
  • 19
  • 33
0

You can define return type as :

public class GraphSharePerformance extends AsyncTask<Void, Void, ArrayList<yourObject>> 

And you can return that ArrayList in doInBackground.

At the receiving side, you need to write :

 ArrayList<yourObject> result = new GraphSharePerformance(args.....).get();
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0
**
* How to return the ArrayList from a calling class.
**

public class AsyncTaskReturnArrayListActivity extends Activity implements
    OnClickListener {

/** Button. */
private Button button;

/** listArrayList. */
private ArrayList<String> listArrayList;

/**
 * On Create.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_async_task_return_array_list);

    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);

}

/**
 * On Button On Click, call AsyncTask.
 */
@Override
public void onClick(View v) {
    /**
     * Call Async Task. Pass Calling Class Reference to Async Task. so that
     * once AsyncTask is complete it return result to calling class.
     */

    new SampleAsyncTask(this).execute();
}

/**
 * Async Task.
 */
public class SampleAsyncTask extends
        AsyncTask<Void, Void, ArrayList<String>> {
    private final AsyncTaskReturnArrayListActivity reference;

    /**
     * Constructor. Get the reference of Calling Class.
     *
     * @param reference
     */
    public SampleAsyncTask(AsyncTaskReturnArrayListActivity reference) {
        this.reference = reference;
    }

    /**
     * Pre Execute.
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("Pre Execute");
    }

    /**
     * Do In Background
     */
    @Override
    protected ArrayList<String> doInBackground(Void... params) {
        ArrayList<String> listArrayList = reference.getArrayList();
        return listArrayList;
    }

    /**
     * On Post Execute.
     */
    @Override
    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);
        // On Complete send result to Calling Class.
        reference.onComplete(result);
    }
}

/**
 * Get Array List.
 *
 * @return
 */
public ArrayList<String> getArrayList() {
    ArrayList<String> listArrayList = new ArrayList<String>();
    listArrayList.add("String 1");
    listArrayList.add("String 2");
    listArrayList.add("String 3");
    return listArrayList;
}

/**
 * On Complete. Get's Result From Async Task.
 *
 * @param listArrayList
 */
private void onComplete(ArrayList<String> listArrayList) {
    this.listArrayList = listArrayList;
    System.out.println(this.listArrayList);
}}
Vasanth
  • 6,257
  • 4
  • 26
  • 19