如何在方法DoInBackground中声明变量?

时间:2016-03-28 10:57:57

标签: android android-asynctask

这是我的代码:

         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 !! 因为它在集团内部宣布了{}。 该做什么??!

5 个答案:

答案 0 :(得分:0)

传递变量onPostExecute

更改Async Task的签名:

class OpenERP extends AsyncTask<Context, Void, Integer>

id中返回doInBackground

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

希望这会帮助你!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

简单地声明为全局变量并在本地初始化。

Configuration

或者您可以将异步任务中的参数更改为

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

允许你让doInBackground返回private class DownloadFilesTask extends AsyncTask<Context, Void, int> 值来捕获onPostExecute中的值。

int

答案 3 :(得分: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。

答案 4 :(得分:-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");
     }
 }