0

What's wrong with this AsyncTask code that prevents it returning an array progress... I get "progress cannot be resolved to a variable" on the noted line.

public class FetchProgress extends AsyncTask<OpportunityActivity, Void, ArrayList<Progress>> {
private OpportunityActivity opportunityActivity;

@Override
protected ArrayList<Progress> doInBackground(OpportunityActivity... activity) {
    opportunityActivity = activity[0];             
    String readFeed = readFeed();
    // this JSON feed is delivered correctly

    try {       
       JSONObject json = new JSONObject(readFeed);                 
       JSONObject jsonObject = new JSONObject(json.optString("ShowProgress"));

          Progress progress = new Progress();
          progress.setShowProgress(jsonObject.getBoolean("ShowProgress"));
          progress.setTarget(jsonObject.getInt("Target"));

     // the above values are set correctly and appear if I use Log            

    } catch (Exception e) {    
        e.printStackTrace();
    }
   // HERE'S THE ISSUE: 
   // Eclipse reports "progress cannot be resolved to a variable"
   return progress;
}

Here's the OnPostExecute:

public void onPostExecute(ArrayList<Progress> progress) {
     opportunityActivity.updateProgress(progress);
}

Here's the class:

public class Progress {
private boolean showprogress;
private int target;

public boolean getShowProgress() {
    return showprogress;
}
public void setShowProgress(boolean showprogress) {
    this.showprogress = showprogress;
}

public int getTarget() {
    return target;
}

public void setTarget(int target) {
    this.target = target;
}
}

Here's the onPostExecute:

public void onPostExecute(ArrayList<Progress> progress) {
     opportunityActivity.updateProgress(progress);
}

And the method it triggers where I can't access the passed ArrayList:

public void updateProgress(ArrayList<Progress> progress) {      
    progress_text  = (TextView)findViewById(R.id.progress_text);          
    progress_text.setText("Target: " + progress.getTarget());
}
1

2 Answers 2

2

Cut this line,

 Progress progress = new Progress();

and paste it out of try statement.

ArrayList<Progress> progressArray=new ArrayList();

  Progress progress = new Progress();

try {       
       JSONObject json = new JSONObject(readFeed);                 
       JSONObject jsonObject = new JSONObject(json.optString("ShowProgress"));


          progress.setShowProgress(jsonObject.getBoolean("ShowProgress"));
          progress.setTarget(jsonObject.getInt("Target"));
       progressArray.add(progress);
     // the above values are set correctly and appear if I use Log            

    } catch (Exception e) {    
        e.printStackTrace();
    }
   return progressArray;

Since you have declared and initialized it within the try Catch it is not visible to your method.

EDIT

But still it would be wrong since it is just a object of class you are trying to return but whereas your method would expect a ArrayList of type to be returned. So I have edited my answer on how you should return a ArrayList.

Sign up to request clarification or add additional context in comments.

3 Comments

If I move the line Progress progress = new Progress(); outside of the try statement then I get a new error "Type mismatch: cannot convert from Progress to ArrayList<Progress>" for my return progress; line.
Thanks - now the array isn't getting passed through to the onPostExecute and ultimate method that acts on this (see updated code). How can I correctly transfer and then access the passed array variables.
The editor liked ArrayList<Progress> progressArray=new ArrayList<Progress>(); This said I am still not able to access the array in my onPostExecute method updateProgress. Any help appreciated.
0

Thanks to Andro (who will be getting added reputation).

I finally accessed the values in the array using this loop (which seems needless) but it finally got the elements out of this one dimensional array:

   for (Progress i : progress) 
   progress_text.setText(i.getOrange() + " / " + i.getTarget() + " hours");        
    progressHours.setMax(i.getTarget());
    progressHours.setProgress(i.getOrange());           
    }

I'd welcome comments on whether adding this second dimension is necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.