Android 在按钮上调用内部AsyncTask类后,单击AsyncTask类之前执行的代码行

Android 在按钮上调用内部AsyncTask类后,单击AsyncTask类之前执行的代码行,android,android-intent,android-asynctask,Android,Android Intent,Android Asynctask,我正在调用一个名为CallWebservice()的内部AsyncTask类。我想在ArrayList中加载数据,然后借助intent在第二页的自定义listview中显示该数据。但我的问题是,当我点击按钮newCallWebservice().execute(),我的控件进入第二页时,Arraylist中没有任何数据。但当我再次回来并在同一个按钮上执行第二次onClick时,这次数据加载到第二页。我不知道发生了什么事。请帮忙 public void onClick(View v) { i

我正在调用一个名为
CallWebservice()
的内部AsyncTask类。我想在ArrayList中加载数据,然后借助intent在第二页的自定义listview中显示该数据。但我的问题是,当我点击按钮new
CallWebservice().execute()
,我的控件进入第二页时,Arraylist中没有任何数据。但当我再次回来并在同一个按钮上执行第二次onClick时,这次数据加载到第二页。我不知道发生了什么事。请帮忙

public void onClick(View v) {
   if (v == btnSearchAreaWise) {
      selectedAreaId = spinArea.getSelectedItemPosition() + 1;
      new CallWebservice().execute("GetDocListByArea");
      Intent i = new Intent(this, DoctorsList.class);
      startActivity(i);
   }
}

您应该在异步类的onPostExecute方法中启动新的intent。

在CallWebservice Asyntask的onPostExecute方法中编写以下代码

Intent i=new Intent(this, DoctorsList.class);
            startActivity(i);
像这样

@Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            try {
                Intent i=new Intent(this, DoctorsList.class);
                               startActivity(i);
                YourCurrentActivity.this.finish();

            } catch (Exception e) {

            }
        }
官方文件

来自Android文档

执行异步任务时,任务将经历4个步骤:

onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

谢谢你的回答。我做了一点修改,效果很好。只需使用我主要活动的外部类。这个放在这个地方。意向i=新意向(MainActivity.this,DoctorsList.class);星触觉(i);欢迎,如果此答案解决了您的问题,您应该将其标记为答案。