2016-03-28 71 views
-1

这里是我的代码:我如何获得在方法DoInBackground中声明的变量?

  class OpenERP extends AsyncTask<Context, Void, Integer> 
       @Override 
        protected Integer doInBackground(Context ... params) { 

        try{ 

        final Integer id = (Integer) models.execute("execute_kw", asList(
          db, uid, password, 
          "pointage.task", "create", 
          asList(new HashMap() { 
           { 
            put("name", "Hello"); 
           } 


          }) 
        ));} 
        catch (XmlRpcException e) { 
        Log.e(TAG, "Xml Rpc Excception " + e.getMessage()); 
       } 

      return id; 
     } 
     @Override 
     protected void onPostExecute(Integer id) { 
      hello.setText(id); 

     } 
     } 

我想要得到的变量ID在我后面的代码使用。 我得到的错误:无法解析symbil id! 因为它是在集团内部声明的{try}。 要做什么??!

回答

0

传递变量onPostExecute。像这样的Async Task

更改签名:

class OpenERP extends AsyncTask<Context, Void, Integer>

回报iddoInBackground

protected Integer doInBackground(Context... params) { 
    .... 
    return id; 
} 

然后覆盖onPostExecute

protected void onPostExecute(Integer id) { 
    // use id here. 
} 

编辑1:

根据评论,这是你应该如何使用它。

protected Integer doInBackground(Context ... params) { 
       Integer id = Integer.valueOf(0); 
       try{ 

       id = (Integer) models.execute("execute_kw", asList(
         db, uid, password, 
         "pointage.task", "create", 
         asList(new HashMap() { 
          { 
           put("name", "Hello"); 
          } 


         }) 
       ));} 
       catch (XmlRpcException e) { 
       Log.e(TAG, "Xml Rpc Excception " + e.getMessage()); 
      } 

     return id; 
} 

希望这可以帮助你!

+0

是的,我想它就像tht,但变量id是在集成块try {}中声明的,所以当我把返回id,我得到:无法解析可变ID! @RohitArya – Steven7

+0

如果你可以''返回''doInBackground',那么你可以使用它。更新完整的代码,如果你不知道如何做到这一点。 –

+0

我更新我的代码@RohitArya,我该如何改变它,所以我可以使用变量ID? – Steven7

0

将它声明为一个类变量,而不是在方法中声明它。在这种情况下,将其声明为OpenERP类的成员。

-1

将doInBackground的返回类型更改为int,并在doInBackground中返回您的整数值,并且您始终可以在onPostExecute中将其作为int获取。下面的示例,

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 
0

只需声明为全局变量并在本地进行初始化即可。

int id; 
@Override 
protected int doInBackground(Context...params){ 
    id = /** your desired code*/ 
    return id; 
} 

或者,你可能会改变异步任务的参数作为

private class DownloadFilesTask extends AsyncTask<Context, Void, int> 

,让你做你doInBackground返回int值赶在onPostExecute。

@Override 
protected void onPostExecute(int result){ 
//access result ass your int value 
} 
0
class OpenERP extends AsyncTask<Context, Void, Void> { 
    private ERPListener mListener; 
    public OpenERP(ERPListener listener){ 
      this.mListener = listener; 
    } 
    @Override 
    protected Void doInBackground(Context... params) { 

       ..... 

    final Integer id = ... 
    mListener.onCompleted(id); 

    return null; 
    } 
} 

其中

public interface ERPListener{ 
    public void onCompleted(int id); 
} 

当您创建异步任务,你所提供的任务完成时将要执行的监听器。它提供了你需要的id给调用线程。