5

在我的應用程序中,我有一個listview,每個項目都有一個按鈕。如果用戶點擊按鈕,我想執行一些http連接。所以我在Adapter類中使用AsyncTask。現在問題是進展對話框沒有顯示。Android:ProgressDialog沒有顯示在Adapter類中

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

     } 
    } 
+0

被onPreExcute()調用的方法?我們需要mor信息。 –

+0

@LazyNinja是onPreExcute()調用。我在lagcat中看到它 – Sridhar

+0

爲什麼不在preExecute方法內的catch塊中記錄異常。如果你在那裏得到例外,你會知道問題是什麼。 – Sameer

回答

-1

。的確,ProgressDialog.show()方法返回一個ProgressDialog obect,但最好先創建一個像ProgressDialog的對象:

ProgressDialog progressDialog1 = new ProgressDialog(); 

然後

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

並且也嘗試通過調用dismiss()方法完成progressDialog1onPostExecute()

+0

我無法執行progressDialog1 = new ProgressDialog()this,因爲它的語法是新的ProgressDialog(上下文)。所以我把它放在構造函數中。但仍progressdialog不顯示 – Sridhar

+0

這裏,而不是私人ProgressDialog progressDialog1;使用私人ProgressDialog progressDialog1 =新的ProgreassDialog(YourParentActivity.this);並調用progressDialog1 as this.progressDialog.show(); – Shrikant

+0

我使用這個適配器類爲兩個活動,所以我怎麼能得到父活動 – Sridhar

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

我將AsyncTask放在一個適配器類中,並且此適配器類是獨立的 – Sridhar

+0

k得到了您的觀點 –

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

@覆蓋 保護無效onPreExecute(){

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

    }