2011-10-19 35 views
0

如何顯示進度條我打電話意圖從A級到B級。 在哪個B級從使用解析手段加載數據,而 發生這種情況我收到空白屏幕在這個時候我想顯示 progressbar我該怎麼做。如何在從遠程數據庫加載數據時在android中顯示進度條?

現在,我寫的代碼如下..

class A extends Activity{ 
    oncreate(){ 
     ... 
    Button b1 = (Button) findViewById(R.id.button1); 

    b1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      new performBackgroundtask().execute(); 
     } 
    } 

    class performBackgroundtask extends AsyncTask<Void, Void, Void> { 
     // 
     ProgressDialog progressDialog = new ProgressDialog(Main.this); 

     @Override 
     protected void onPreExecute() { 
      progressDialog = ProgressDialog.show(Main.this,"", "Please wait...");     
      super.onPreExecute(); 

      /* 
      * progressDialog.setMessage("Please wait while loading ..."); 
      * progressDialog.show(); progressDialog.setCancelable(true); 
      */   
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      Intent in = new Intent(A.this, B.class);      
      startActivity(in); 
      // TODO Auto-generated method stub   
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      progressDialog.dismiss();  
     } 
    } 
} 

class B { ... 
/* 
* In which this class has another view which has to get the data from 
* URL using SAXPArsing. 
* 
* ... for this where i need to write progress bar code. 
*/ 
} 
+0

你在做AsyncTask的doInBackground(Void ... params)方法嗎? – user370305

+0

意圖調用另一個需要從服務器加載數據的類。 – Harish

回答

1

我已經用處理程序中完成類似的東西。當處理程序收到來自線程的響應時,可以關閉進度對話框。

一些代碼,以幫助您瞭解:

final Handler handler = new Handler() 
    { 
     @Override 
     public void handleMessage(Message message) 
     { 
      progressBar.setVisibility(View.GONE); 
      // My irrelevant code here 
     } 
    }; 

    new Thread(new Runnable() 
    { 
     public void run() 
     { 
      // Do your stuff here 
      Message message = handler.obtainMessage(1, text); 
      handler.sendMessage(message); 
     } 
     }).start(); 
0

而不是調用另外一個Activity類做你的下載,移動這個功能到另一個類,每種活動都可以使用。

相關問題