2011-09-08 76 views
2

在我的應用程序中,當我點擊按鈕時,進度條將最近啓動。我想快速開始。請任何人都可以幫助我。 onCreate()中的bshowgifts監聽器。android-進度條延遲加載,當點擊按鈕時

bShowGifts.setOnClickListener(new OnClickListener() 
{       

@Override   
     public void onClick(View v) 
     {    
      //vsChange 
      progressDlg = ProgressDialog.show(Ehome.this, "", "", true);        
      progressDlg.setContentView(R.layout.custom_dialog);     
      //Gender and age must be selected 
      if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0) 
      { 
       progressDlg.dismiss(); 
       String strMsg = "Please select a gender"; 
       DisplaySelectionMessage(strMsg);    
       return; 
      } 

      if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0) 
      { 
       progressDlg.dismiss(); 
       String strMsg = "Please select an age"; 
       DisplaySelectionMessage(strMsg);    
       return; 
      } 

      if(nMatchingGifts == 0) 
      { 
       progressDlg.dismiss(); 
       tvCount = (TextView) findViewById(R.id.textMatchinGifts); 
       String strContent = "# of Matching Gifts: 0"; 
       tvCount.setText(strContent);               
       String strMsg = "No gifts found! Please change the search criteria."; 
       DisplaySelectionMessage(strMsg);    
       return; 
      }    

      try 
      {     
       //Thread for getting the negative attributes values 
       Thread tDisplayCategories = new Thread() 
       { 
        public void run() 
        { 


         System.out.println("Showgifts : Thread:run"); 
         handlerDisplayGifts.post(call_ShowGiftsCategoryPage); 
        }                       
       }; 
       tDisplayCategories.start(); 
      } 
      catch(Exception exp) 
      { 
       progressDlg.dismiss(); 
      } 
      return; 
      }   
    }); 

    public void DisplaySelectionMessage(String strMsg) 
{ 

      //display alert dialog    
    AlertDialog alertDialog = new AlertDialog.Builder(Ehome.this).create(); 
    alertDialog.setTitle("Elgifto Alert"); 
    alertDialog.setMessage(strMsg); 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
    {   
     public void onClick(DialogInterface dialog, int id) 
     {     
      dialog.cancel(); 
     } 
    }); 

    alertDialog.show(); 
    return; 
} 

更新代碼

bShowGifts.setOnClickListener(new OnClickListener() 

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

public class showgiftsbtn extends AsyncTask<Void, Void, Void> 

{ 

private final ProgressDialog dialog = new ProgressDialog(Ehome.this); 

     protected void onPreExecute() 
     { 
      this.dialog.setMessage("Please Wait..."); 
      this.dialog.show(); 



     // put your code which preload with processDialog 
     if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0) 
     { 
      dialog.dismiss(); 
      String strMsg = "Please select a gender"; 
      DisplaySelectionMessage(strMsg);    
      return; 
     } 

     if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0) 
     { 
      dialog.dismiss(); 
      String strMsg = "Please select an age"; 
      DisplaySelectionMessage(strMsg);    
      return; 
     } 

     if(nMatchingGifts == 0) 
     { 
      dialog.dismiss(); 
      tvCount = (TextView) findViewById(R.id.textMatchinGifts); 
      String strContent = "# of Matching Gifts: 0"; 
      tvCount.setText(strContent);               
      String strMsg = "No gifts found! Please change the search criteria."; 
      DisplaySelectionMessage(strMsg);    
      return; 
     }    

     try 
     {     
      //Thread for getting the negative attributes values 
      Thread tDisplayCategories = new Thread() 
      { 
       public void run() 
       { 
        System.out.println("Showgifts : Thread:run"); 
        handlerDisplayGifts.post(call_ShowGiftsCategoryPage); 
        dialog.dismiss(); 
       }                       
      }; 
      tDisplayCategories.start(); 
     } 
     catch(Exception exp) 
     { 
      dialog.dismiss(); 
     } 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) 
    {   
     // put you code here 
     return null; 
    } 
    protected void onPostExecute(final Void unused) 
    {   

    } 
} 

回答

4

嘗試這種:: 的AsyncTask能夠正確且容易使用的UI線程。該類允許執行後臺操作並在UI線程上發佈結果,而無需操縱線程和/或處理程序。

異步任務由在後臺線程上運行並且其結果在UI線程上發佈的計算定義。異步任務由3種泛型類型,稱爲參數,可以進展和結果,以及4個步驟定義,所謂onPreExecute,doInBackground,onProgressUpdate和onPostExecute

private class xyz extends AsyncTask<Void, Void, Void> { 
       private final ProgressDialog dialog = new ProgressDialog(tranning.this); 

       protected void onPreExecute() { 
        this.dialog.setMessage("Please Wait..."); 
        this.dialog.show(); 

        // put your code which preload with processDialog 


      @Override 
      protected Void doInBackground(Void... arg0) { 

       // put you code here 
if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0) 
      { 

       String strMsg = "Please select a gender"; 
       DisplaySelectionMessage(strMsg);    
       return; 
      } 

      if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0) 
      { 
       progressDlg.dismiss(); 
       String strMsg = "Please select an age"; 
       DisplaySelectionMessage(strMsg);    
       return; 
      } 

     if(nMatchingGifts == 0) 
     { 

      tvCount = (TextView) findViewById(R.id.textMatchinGifts); 
      String strContent = "# of Matching Gifts: 0"; 
      tvCount.setText(strContent);               
      String strMsg = "No gifts found! Please change the search criteria."; 
      DisplaySelectionMessage(strMsg);    
      return; 
     }    

     try 
     {     
      //Thread for getting the negative attributes values 
      Thread tDisplayCategories = new Thread() 
      { 
       public void run() 
       { 


        System.out.println("Showgifts : Thread:run"); 
        handlerDisplayGifts.post(call_ShowGiftsCategoryPage); 
       }                       
      }; 
      tDisplayCategories.start(); 
     } 
     catch(Exception exp) 
     { 

     } 
      } 

       return null; 
      } 
protected void onPostExecute(final Void unused) { 
       if (this.dialog.isShowing()) { 
        this.dialog.dismiss(); 

       } 

      } 
     } 

並在按鈕單擊事件::

使用
new xyz().execute(); 
+0

單擊事件表示單擊偵聽器。這是對的嗎?我把這樣的bShowGifts.setOnClickListener(新OnClickListener() \t \t { \t \t \t @覆蓋 \t \t \t公共無效的onClick(視圖v) \t \t \t {\t \t \t \t \t新showgiftsbtn()。執行(); \t} \t \t}); – naresh

+0

這是行不通的 – naresh

+0

public void onClick(View v) { new xyz()。execute(); –

0

使用Handler顯示ProgressBar() ....

private Handler mHandler = new Handler(); 

final Runnable mUpdateResults = new Runnable() { 
     public void run() { 
     progressDlg = ProgressDialog.show(Ehome.this, "", "", true);        
      progressDlg.setContentView(R.layout.custom_dialog); 
     } 
}; 

把上面的代碼上面的類代碼並稱之爲其中顯示進度條( );像這樣:

mHandler.post(mUpdateResults); 
+0

如何在這裏使用? – naresh

+0

我已經編輯了答案,請檢查。 –

+0

它不起作用。是mhandler.post(mupdateresults)放入線程? – naresh