0

私のコードでは、Async Task を使用してスピナー アダプターをロードします。アダプターのロード後にアイテムを表示したいのですが、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 でプログラミングを行う場合、画面に何かを描画するタスクはメイン スレッドで実行する必要があることを 1 つ覚えておく必要があります。アダプターを設定すると、Android はアダプターの getView() メソッドを呼び出し、画面にビューを描画します。そのため、doInBackground() メソッドではなく、postExecute() メソッドでアダプターを設定する必要があります。
私の要点を明確にするための小さなサンプルを次に示します。

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. やりたいプロセスで非同期クラスを作成し、UIを変換するものは何もありません
  2. OnAsyncTaskComplete(Object response) のような、非同期タスクの終了時に UI を変更する UI スレッドの関数を作成します。
  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 に答える