如何通过使用异步任务类设置微调的适配器 [英] How to set adapter of spinner by using Async Task Class

查看:120
本文介绍了如何通过使用异步任务类设置微调的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code我用异步任务加载微调适配器     在我的情况下ProgressDialog是不是不屑一顾     这是我的code。     我想显示适配器后负荷的项目和progressDialog是解雇     请帮帮我,谢谢

 私有类LoadMoreVehicals扩展的AsyncTask<对象,整数,对象> {

        @覆盖
        在preExecute保护无效(){

            进度= ProgressDialog.show(RegistrationScreen.this,,
                    载入中...);
            progressBar.setIndeterminate(真正的);
            progressBar.setIndeterminateDrawable(getResources()。getDrawable(
                    R.anim.progressbar_handler));
            super.on preExecute();
        }

        @覆盖
        保护对象doInBackground(对象... PARAMS){
            字符串countryUrl = ConstantURL.COUNTRY_URL;
            getCounty(countryUrl);

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

                                返回null;
        }

        @覆盖
        保护无效onProgressUpdate(整数...值){
            progressBar.getProgress();

        }

        @覆盖
        保护无效onPostExecute(对象结果){

            progressBar.dismiss();

             Log.e(我现在在onPostExecute,);
            super.onPostExecute(结果);
        }
    }
 

解决方案

在我的经验,我有这么多的问题与异步运行和界面,因此现在总是分开的东西试图将责任在每个地方。所以我做这样的事情:

  1. 在与我想做的事情,没有什么的过程,改造UI在里面创建我的异步类
  2. 创建在UI线程的函数修改UI的时候异步任务的完成,像OnAsyncTaskComplete(对象响应)
  3. 请传达的主题

     公共类MyActivity延伸活动{
    
    私有静态MyAsyncClass backgroundTask;
    私有静态ProgressDialog pleaseWaitDialog;
    
    //......activity东西.......
    
    @覆盖
    公共无效的onPause()
    {
        super.onPause();
        //获取在屏幕旋转或其它状态改变的情况下,摆脱进度对话框。 prevents崩溃。
        如果(pleaseWaitDialog!= NULL)
            pleaseWaitDialog.dismiss();
    }
    //功能,避免失去异步线程,如果应用程序中断(电话旋转,来电通等)RECOMENDED来处理这个!
    //后的应用程序恢复设置当前状态
    @覆盖
    公共无效onResume()
    {
    
        super.onResume();
        //如果有一个后台任务将其设置为新的活动
        如果((backgroundTask = NULL)及!及(backgroundTask.getStatus()== Status.RUNNING))
        {
           如果(pleaseWaitDialog!= NULL)
             pleaseWaitDialog.show();
    
           backgroundTask.setActivity(本);
        }
        }
    }
    
        //逻辑业务后的W​​eb服务完整的在这里
    //请在修改UI在这样的功能的东西
    私人无效onTaskCompleted(对象_response)
    {
        //例如_response可以是一个新的适配器
    MyList.setAdapter((BaseAdapter)_response);
    //或可以是一个列表,以创建新的适配器
    MyList.setAdapter(新MyAdapter(这一点,(ArrayList的<字符串>)_响应));
    //也可以是任何你想要的,只是尽量让在这里,你需要更改用户界面的东西
    }
    
    / **
     *类处理异步任务
     * /
    公共类MyAsyncClass扩展的AsyncTask<虚空,虚空,对象>
    {
        //维护连接活动状态的改变建议
         私人MyActivity活动;
         //保持异步任务的响应
         私有对象_response;
         //标志是守异步任务完成情况
         私人布尔完成;
    
         //构造函数
         私人MyAsyncClass(MyActivity活动){
                 this.activity =活动;
         }
    
         // pre执行行动
         @覆盖
         在preExecute保护无效(){
                 //启动闪屏对话框
                 如果(pleaseWaitDialog == NULL)
                     pleaseWaitDialog = ProgressDialog.show(activity.this,
                                                            请稍候,
                                                            获得结果...
                                                            假);
    
         }
    
         //异步任务的执行
        保护对象doInBackground(对象... PARAMS)
        {
            //返回你想要的东西还是想你想
            返回新的ArrayList();
        }
    
        //邮执行行动
        @覆盖
        保护无效onPostExecute(对象响应)
        {
            //设置任务已完成,并通知活动
                完成=真;
                _response =响应;
                notifyActivityTaskCompleted();
            //关闭闪屏
            如果(pleaseWaitDialog!= NULL)
            {
                pleaseWaitDialog.dismiss();
                pleaseWaitDialog = NULL;
            }
        }
    
        //通知的异步任务完成活动
        私人无效notifyActivityTaskCompleted()
        {
            如果(NULL!=活动){
                activity.onTaskCompleted(_response);
            }
        }
    
    //为维护连接异步任务活动在手机状态变化
        //将当前活动的异步任务
        公共无效setActivity(MyActivity活动)
        {
            this.activity =活动;
            如果(完成){
                notifyActivityTaskCompleted();
            }
        }
    
    }
    }
     

希望它帮助你。

In my code I load a spinner adapter by using Async Task In My case The ProgressDialog is Not dismissing This is My code. I want to show the item after adapter load and the progressDialog is to dismiss Please Help me, Thanks

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

解决方案

In my experience i have so many problems with async runs and UI so now always separate the stuff trying to place the "responsibilities" in each place. So i do something like this:

  1. Create my Async class with the process i want to do and nothing that transform the UI in it
  2. Create a function in UI thread that modify the UI when async task finish, something like OnAsyncTaskComplete(Object response)
  3. Keep communicated the threads

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

Hope its help you

这篇关于如何通过使用异步任务类设置微调的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆