【问题标题】:Android: ProgressDialog not showing in Adapter ClassAndroid:ProgressDialog 未显示在适配器类中
【发布时间】:2012-08-22 14:49:58
【问题描述】:

在我的应用程序中,我有一个列表视图,每个项目都有一个按钮。如果用户单击按钮,我想执行一些 http 连接。所以我在 Adapter 类中使用 AsyncTask。现在的问题是进度对话框没有显示。

private class MyClass extends AsyncTask<Void, Long, Boolean> {
        private Context context;
        private ServerCall call = new ServerCall();
        private ProgressDialog progressDialog1;

        public MyClass(Context context) {
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                progressDialog1 = ProgressDialog.show(context, "",
                        "Please wait...", true);
                progressDialog1.setIndeterminate(true);

            } catch (final Throwable th) {

            }

        }

        @Override
        protected void onProgressUpdate(Long... values) {

            super.onProgressUpdate(values);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            //some tasks

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (mode.equalsIgnoreCase("History")) {

                Intent order = new Intent(context, ActicityA.class);
                order.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(order);

            } else {
                Intent order = new Intent(context, ActivityB.class);
                order.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(order);
            }

        }
    }

【问题讨论】:

  • 是否调用了 onPreExcute() 方法?我们需要铁道部信息。
  • @LazyNinja 是 onPreExcute() 调用。我在 lagcat 看到它
  • 为什么不在 preExecute 方法内的 catch 块中记录异常。如果您在那里遇到异常,您就会知道问题所在。

标签: android android-asynctask android-adapter android-context


【解决方案1】:

ProgressDialog.show() 方法确实返回一个ProgressDialog 对象,但最好先创建一个 ProgressDialog 对象,例如:

ProgressDialog progressDialog1 = new ProgressDialog();

然后

progressDialog1 = ProgressDialog.show(context, "",
                        "Please wait...", true);

另外,尝试通过调用onPostExecute() 中的dismiss() 方法来完成progressDialog1

【讨论】:

  • 我无法执行 progressDialog1 = new ProgressDialog() this ,因为它的语法是 new ProgressDialog(context)。所以我把它放在构造函数中。但进度对话框仍然没有显示
  • 这里代替私有 ProgressDialog progressDialog1;使用私有 ProgressDialog progressDialog1 = new ProgreassDialog(YourParentActivity.this);并将 progressDialog1 称为 this.progressDialog.show();
  • 我将这个适配器类用于两个活动,那么我如何获得父活动
  • 好的,但是将上下文传递给 ProgressDialog 也应该可以。这个 AsyncTask 在 Adapter 里面吗?
  • 好吧,那就奇怪了。现在您是否在 Adapter 类中调用您的 execute() 方法。你不应该在那里调用它,如果可能的话,将 AsyncTask 和 Adapter 类分开。您是否尝试过我上面提到的解决方案?如果仍然没有解决,那么在调用 execute() 方法之前,启动 progressDialog 并在从 onPostExecute() 返回时关闭 progressDialog。
【解决方案2】:

试试这个吧'

protected void onPreExecute() {
        dialog = new ProgressDialog(Activity.this);
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        //dialog.setCancelable(false);
        dialog.show();
    }

【讨论】:

    【解决方案3】:
        Follow this steps 
        [0] start showing progressbar
        [1] Execute AsyncTask class
        [2] in this class one interface for post the response in main class 
        [3] Declare one interface class
        [4] get Reponse in Main Clss using interface 
        [5] dismiss Progressbar and Bind your Data 
    
        ex. 
        [0]
    
                progr.setVisibility(View.VISIBLE);
        [1]
                        MyTask task = new MyTask();
                    task.setdata(new SendData() {
        [4]             @Override
                        public void GetStringResponse(String str) {
                            Log.i(General.TAG, TAG + " Overide GetString-"+str);                    
                            final String GetResponse=str;
                        // here you get response and parse it   
       [5]                               // here you can dismiss your progress bar                          
                        }
                    });
                    task.execute(new String[] {Newsurl[Id]});
    
    
        [2] Add interface here with Method
    
        SendData  objsend;
    
        @Override 
            protected void onPostExecute(String Result)
            {
                Log.i("**********", Result);        
                objsend.GetStringResponse(Result);  
            }
    
            public void setdata(SendData  sendd)
            {
                Log.i(General.TAG,TAG+"Initialise Interface object");
                objsend = sendd;
            }
    
     [3] interface class
         public interface SendData {    
        public  abstract void GetStringResponse(String str);
    }
    

    【讨论】:

    • 我将 AsyncTask 放在一个适配器类中,并且这个适配器类是独立的
    【解决方案4】:
    <pre><code>
    public MyClass(Context context) {
            this.context = context;
            progressDialog1=new ProgressDialog(context);
        }
    

    @覆盖 受保护的无效 onPreExecute() {

        progressdialog1.setMessage("please wait while loading");
        progressdialog1.setIndeterminate(true);
    
        }  
    

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多