-1

I have an android fragment which contains an android AsyncTask class implemented as follows

    protected class getGraph extends
        AsyncTask<GraphFragment, Void, GraphFragment> {
    @Override
    protected GraphFragment doInBackground(GraphFragment... params) {
        performance = getPerformance(sharename);
        return null;
    }

    protected void onPostExecute(GraphFragment Param) {

    }

}

Now i want to return the value performance which is an ArrayList to the calling method.How do i achieve that?

mungaih pk
  • 1,809
  • 8
  • 31
  • 57
  • 1
    use a interface look at the answer by blackbelt http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask – Raghunandan Oct 18 '13 at 09:32
  • Now i want to return the value performance which is an ArrayList to the calling method. what is the calling method here? i don't understand – Raghunandan Oct 18 '13 at 09:33

4 Answers4

0

Try this:

(replace "String" with whatever native variable type your data is)

protected class getGraph extends
    AsyncTask<GraphFragment, Void, List<String>> {

List<String> performance = new ArrayList<String>();

@Override
protected GraphFragment doInBackground(GraphFragment... params) {
    performance = getPerformance(sharename);
    return null;
}

protected void onPostExecute(List<String> Param) {

}

}

-1

Take this aproach

protected class getGraph extends
    AsyncTask<GraphFragment, Void, ArrayList<YOUROBJECT> {

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

    public void onPostExecute( ArrayList<YOUROBJECT> Param) {

    }
}

In your other Class/MainActivity call it this way:

private void getSomething(){
    new getGraph({
        @Override
        void onPostExecute(ArrayList<YOUROBJECT> Param){
            //DO SOMETHING IN MAINACTIVITY
        }
    }).execute(); 
}
A.S.
  • 4,574
  • 3
  • 26
  • 43
  • I have tried that and now am getting a syntax error in "protected GraphFragment doInBackground()".The error is that i should change the return type of the method.Is it ok if i change the implementation to this "protected ArrayList doInBackground(GraphFragment... params)"? – mungaih pk Oct 18 '13 at 09:45
  • How do i get the returned value?I am calling the class from another method. – mungaih pk Oct 18 '13 at 10:11
-1

Do this way..

protected class getGraph extends
    AsyncTask<GraphFragment, Void, ArrayList<String> {


    @Override
    protected GraphFragment doInBackground(GraphFragment... params) {
       performance = getPerformance(sharename);
       ArrayList<String> DATA=new ArrayList<String>();
       /*
       do the process and assign result in DATA
       */
       return DATA;
    }

    protected void onPostExecute( ArrayList<String> Param) {
     // Get your arraylist here.
    }
}
Brijesh Patel
  • 676
  • 1
  • 5
  • 13
-1

Use following code.

public class calc_stanica extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(baraj_mapa.this);
        dialog.setTitle("Calculating...");
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
    }

    protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
        ArrayList<String> result = new ArrayList<String>();
        ArrayList<String> passed = passing[0]; //get passed arraylist

        // Assign data to your Arralist from your method
         result = getData();
        //Some calculations...do your stuff here.

        return result; //return result arraylist
    }

    protected void onPostExecute(ArrayList<String> result) {
        dialog.dismiss();
       // Get your arraylist here.
    }
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36