Reputation: 366
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());
}
Upvotes: 0
Views: 717
Reputation: 366
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.
Upvotes: 0
Reputation: 54322
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.
Upvotes: 2