Android:Adapter类中未显示ProgressDialog

5

在我的应用程序中,我有一个列表视图,每个项目都有一个按钮。如果用户点击按钮,我想执行一些http连接。因此,在适配器类中使用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);
            }

        }
    }

onPreExecute()方法被调用了吗?我们需要更多的信息。 - Lazy Ninja
@LazyNinja 是的,onPreExecute() 被调用了。我在 logcat 中看到了。 - Sridhar
为什么不在preExecute方法的catch块中记录异常。如果你在那里得到了一个异常,你就会知道问题出在哪里。 - Sameer
4个回答

0

试试这个替代方案'

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

0
    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放置在一个适配器类中,而这个适配器类是独立的。 - Sridhar

0
<pre><code>
public MyClass(Context context) {
        this.context = context;
        progressDialog1=new ProgressDialog(context);
    }

@覆盖 保护的 空 onPreExecute() {

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

    }  


-1

虽然ProgressDialog.show()方法会返回一个ProgressDialog对象,但是最好先创建一个ProgressDialog对象:

ProgressDialog progressDialog1 = new ProgressDialog();

然后

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

此外,在onPostExecute()中调用progressDialog1dismiss()方法,尝试完成进度对话框。

我无法执行progressDialog1 = new ProgressDialog(),因为它的语法是new ProgressDialog(context)。所以我把它放在构造函数中。但是进度对话框仍然没有显示。 - Sridhar
好的,但将上下文传递给ProgressDialog也应该可以工作。这个AsyncTask是在Adapter内部吗? - Shrikant Ballal
好的,那么奇怪了。现在你是否在适配器类中调用了execute()方法?你不应该在那里调用它,如果可能的话,分离AsyncTask和Adapter类。你尝试过我上面提到的解决方案吗?如果仍然无法解决问题,在调用execute()方法之前启动progressDialog,在onPostExecute()返回后关闭progressDialog。 - Shrikant Ballal
我刚刚通过意图调用了另一个活动,在那里我使用了AsyncTask。 - Sridhar
我仍然收到异常。我真的不明白为什么这个答案被接受了。 - Shekh Shagar
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接