0

在我的代码中,我使用异步任务加载微调器适配器在我的情况下,ProgressDialog 没有关闭这是我的代码。我想在适配器加载后显示项目,progressDialog 是关闭请帮助我,谢谢

private class LoadMoreVehicals extends AsyncTask<Object, Integer, Object> {

        @Override
        protected void onPreExecute() {

            progressBar = ProgressDialog.show(RegistrationScreen.this, "",
                    "Loading...");
            progressBar.setIndeterminate(true);
            progressBar.setIndeterminateDrawable(getResources().getDrawable(
                    R.anim.progressbar_handler));
            super.onPreExecute();
        }

        @Override
        protected Object doInBackground(Object... params) {
            String countryUrl = ConstantURL.COUNTRY_URL;
            getCounty(countryUrl);

            countrySpinner
            .setAdapter(new MyCustomSpinnerAdapter(
                    RegistrationScreen.this,
                    R.layout.spinner_dropdown,
                    countyList));

                                return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            progressBar.getProgress();

        }

        @Override
        protected void onPostExecute(Object result) {

            progressBar.dismiss();

             Log.e("Im in onPostExecute", "");
            super.onPostExecute(result);
        }
    } 
4

3 回答 3

2

在 Android 中编程时,您应该记住一件事,任何在屏幕上绘制内容的任务都应该在主线程上执行。当您设置适配器时,android 会调用适配器的 getView() 方法并在屏幕上绘制视图。所以你应该在 postExecute() 方法中设置适配器,而不是在 doInBackground() 方法中。
这是一个小样本来澄清我的观点:

class MyTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute ( )
    {
        //starting the progress dialogue
        pd.show();
    }

    @Override
    protected Void doInBackground (Void... params)
    {
        //fetch data here
        ...
        ... 
        return null;
    }

            @Override
    protected void onPostExecute (Void result)
    {
        //set adapter here
        ...
        ...
        //dismissing the progress dialogue
        pd.dismiss();
    }

}

于 2012-09-19T12:43:10.523 回答
0

首先,您不能在doInBackground 以下设计中设置适配器:

private class LoadMoreVehicals extends AsyncTask<Object, Integer, Object> 
{
    private ArrayList<Country> countries;
    @Override
    protected void onPreExecute() {
         progressBar = ProgressDialog.show(RegistrationScreen.this, "","Loading...");
         progressBar.setIndeterminate(true);
         progressBar.setIndeterminateDrawable(getResources().getDrawable(R.anim.progressbar_handler));              
         super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Object... params) {
        String countryUrl = ConstantURL.COUNTRY_URL;
        countries = getCounty(countryUrl);
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressBar.getProgress();

    }

    @Override
    protected void onPostExecute(Object result) {
        countrySpinner.setAdapter(new MyCustomSpinnerAdapter(RegistrationScreen.this,R.layout.spinner_dropdown,countries));
        progressBar.dismiss();
        Log.e("Im in onPostExecute", "");
        super.onPostExecute(result);
    }
} 
于 2012-09-19T12:05:05.670 回答
0

根据我的经验,我在异步运行和 UI 方面遇到了很多问题,所以现在总是将试图将“职责”放在每个地方的东西分开。所以我做这样的事情:

  1. 使用我想要做的过程创建我的 Async 类,并且没有任何东西可以改变其中的 UI
  2. 在异步任务完成时修改 UI 的 UI 线程中创建一个函数,例如 OnAsyncTaskComplete(Object response)
  3. 保持沟通线程

    public class MyActivity extends Activity {
    
    private static MyAsyncClass backgroundTask;
    private static ProgressDialog pleaseWaitDialog; 
    
    //......activity stuff.......
    
    @Override
    public void onPause()
    {
        super.onPause();
        //Get rid of progress dialog in the event of a screen rotation or other state change. Prevents a crash.
        if (pleaseWaitDialog != null)
            pleaseWaitDialog.dismiss();
    }
    //Function to avoid lose the async thread if the app interrupts (phone rotation, incoming call, etc) RECOMENDED TO HANDLE THIS!!
    //Sets the current state after app resume
    @Override
    public void onResume()
    {
    
        super.onResume();
        //If there is a background task set it to the new activity
        if ((backgroundTask != null) && (backgroundTask.getStatus() == Status.RUNNING))
        {
           if (pleaseWaitDialog != null)
             pleaseWaitDialog.show();
    
           backgroundTask.setActivity(this);
        }
        }
    }
    
        //Logic business after the web service complete here
    //Do the thing that modify the UI in a function like this
    private void onTaskCompleted(Object _response) 
    { 
        //For example _response can be a new adapter
    MyList.setAdapter((BaseAdapter)_response);
    //or can be a list to create the new adapter
    MyList.setAdapter(new MyAdapter(this, (ArrayList<String>)_response));
    //or can be anything you want, just try to make here the things that you need to change the UI
    }
    
    /**
     * Class that handle the async task
     */
    public class MyAsyncClass extends AsyncTask<Void, Void, Object>
    {
        //Maintain attached activity for states change propose 
         private MyActivity activity;
         //Keep the response of the async task
         private Object _response;
         //Flag that keep async task completed status
         private boolean completed; 
    
         //Constructor
         private MyAsyncClass(MyActivity activity) { 
                 this.activity = activity;
         } 
    
         //Pre execution actions
         @Override 
         protected void onPreExecute() {
                 //Start the splash screen dialog
                 if (pleaseWaitDialog == null)
                     pleaseWaitDialog= ProgressDialog.show(activity.this, 
                                                            "PLEASE WAIT", 
                                                            "Getting results...", 
                                                            false);
    
         } 
    
         //Execution of the async task
        protected Object doInBackground(Object...params)
        {
            //return the thing you want or do want you want
            return new ArrayList();
        }
    
        //Post execution actions
        @Override 
        protected void onPostExecute(Object response) 
        {
            //Set task completed and notify the activity
                completed = true;
                _response = response;
                notifyActivityTaskCompleted();
            //Close the splash screen
            if (pleaseWaitDialog != null)
            {
                pleaseWaitDialog.dismiss();
                pleaseWaitDialog = null;
            }
        } 
    
        //Notify activity of async task completion
        private void notifyActivityTaskCompleted() 
        { 
            if ( null != activity ) { 
                activity.onTaskCompleted(_response); 
            } 
        } 
    
    //for maintain attached the async task to the activity in phone states changes
        //Sets the current activity to the async task
        public void setActivity(MyActivity activity) 
        { 
            this.activity = activity; 
            if ( completed ) { 
                notifyActivityTaskCompleted(); 
            } 
        } 
    
    }
    }
    

希望它对你有帮助

于 2012-09-19T14:45:45.487 回答