5

Trong ứng dụng của tôi, tôi có một listview và mỗi mục có một nút. Nếu người dùng nhấp vào nút, tôi muốn thực hiện một số kết nối http. Vì vậy, tôi sử dụng AsyncTask trong lớp Adapter. Bây giờ vấn đề là hộp thoại tiến trình không hiển thị.Android: ProgressDialog không hiển thị trong Bộ điều hợp Class

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

được onPreExcute() phương pháp được gọi là? Chúng ta cần thông tin mor. –

+0

@LazyNinja yes onPreExcute() được gọi. tôi thấy nó trong lagcat – Sridhar

+0

Tại sao bạn không đăng nhập ngoại lệ trong khối catch bên trong phương thức preExecute. Nếu bạn đang nhận được một ngoại lệ ở đó, bạn sẽ biết vấn đề là gì. – Sameer

Trả lời

-1

của nó đúng là ProgressDialog.show() phương thức trả về một obect ProgressDialog, nhưng nó tốt hơn để lần đầu tiên tạo một đối tượng của ProgressDialog như:

ProgressDialog progressDialog1 = new ProgressDialog(); 

và sau đó

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

Và cũng có thể, cố gắng hoàn thành progressDialog1 trong onPostExecute() bằng cách gọi phương thức dismiss() của nó.

+0

Tôi không thể thực hiện tiến trìnhDialog1 = new ProgressDialog() điều này, bởi vì cú pháp của nó là ProgressDialog (ngữ cảnh) mới. vì vậy tôi đặt nó vào hàm tạo. Nhưng vẫn progressdialog không hiển thị – Sridhar

+0

Ở đây thay vì tiến bộ ProgressDialog riêngDialog1; sử dụng tiến trình ProgressDialog riêngDialog1 = new ProgreassDialog (YourParentActivity.this); Và gọi tiến trìnhDialog1 là this.progressDialog.show(); – Shrikant

+0

Tôi sử dụng lớp bộ điều hợp này cho hai hoạt động, vì vậy làm thế nào tôi có thể có được hoạt động của cha mẹ – Sridhar

0

Hãy thử điều này thay vì'

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

Tôi đặt AsyncTask bên trong một lớp bộ điều hợp, Và lớp bộ điều hợp này là độc lập – Sridhar

+0

k có quan điểm của bạn –

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

@ Override protected void onPreExecute() {

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

    } 

Các vấn đề liên quan