5

Nella mia applicazione, ho un listview e ogni elemento ha un pulsante. Se l'utente fa clic sul pulsante, voglio eseguire qualche connessione http. Quindi io uso AsyncTask nella classe Adapter. Ora il problema è che la finestra di avanzamento non viene mostrata.Android: ProgressDialog non viene visualizzato nella classe adattatore

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

stato onPreExcute() metodo chiamato? Abbiamo bisogno di informazioni morali. –

+0

@LazyNinja yes onPreExcute() chiamato. lo vedo in lagcat – Sridhar

+0

Perché non registrare l'eccezione nel blocco catch all'interno del metodo preExecute. Se stai ricevendo un'eccezione, saprai qual è il problema. – Sameer

risposta

-1

E 'vero che ProgressDialog.show() metodo restituisce un obect ProgressDialog, ma il suo meglio per creare prima un oggetto di ProgressDialog come:

ProgressDialog progressDialog1 = new ProgressDialog(); 

e poi

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

E inoltre, cercare di completare il progressDialog1 in onPostExecute() chiamando il suo metodo dismiss().

+0

Non posso eseguire progressDialog1 = new ProgressDialog() perché la sua sintassi è nuova ProgressDialog (context). quindi l'ho messo nel costruttore. Ma ancora progressdialog non mostra – Sridhar

+0

Qui invece di ProgressDialog privato progressDialog1; usa progressDialog privato progressDialog1 = new ProgreassDialog (YourParentActivity.this); E chiama progressDialog1 come this.progressDialog.show(); – Shrikant

+0

Uso questa classe dell'adattatore per due attività, quindi come posso ottenere l'attività principale – Sridhar

0

Prova a modificare la'

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

Posiziono AsyncTask all'interno di una classe di adapter, e questa classe Adapter è indipendente – Sridhar

+0

k ha ottenuto il tuo punto –

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

    } 

Problemi correlati