0

这个 AsyncTask 代码有什么问题,它阻止它返回数组进度......我在注释行上得到“进度无法解析为变量”。

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;
}

这是 OnPostExecute:

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

这是课程:

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;
}
}

这是 onPostExecute:

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

它在我无法访问传递的 ArrayList 时触发的方法:

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

2 回答 2

2

剪掉这条线,

 Progress progress = new Progress();

并将其粘贴到 try 语句中。

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;

由于您已在 try Catch 中声明并初始化它,因此您的方法看不到它。

编辑

但它仍然是错误的,因为它只是您尝试返回的类的对象,而您的方法期望返回类型为 ArrayList 的对象。所以我已经编辑了关于如何返回 ArrayList 的答案。

于 2012-10-22T05:50:21.380 回答
0

感谢 Andro(他将获得更多的声誉)。

我终于使用这个循环访问了数组中的值(这似乎没有必要),但它最终从这个一维数组中取出了元素:

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

我欢迎就添加第二个维度是否有必要发表评论。

于 2012-10-22T06:45:18.987 回答