2011-05-06 140 views
0

我在Android上的C++程序員,新的,我從一個文本文件中使用以下方法下載文本:顯示進度條

public static String DownloadText(String URL) 
    { 
     int BUFFER_SIZE = 2000; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      } 
     catch (IOException e1) 
     { 
     e1.printStackTrace(); 
     return ""; 
     } 

    InputStreamReader isr = new InputStreamReader(in); 
    int charRead; 
     String str = ""; 
     char[] inputBuffer = new char[BUFFER_SIZE];   
    try { 
     while ((charRead = isr.read(inputBuffer))>0) 
     {      
      //---convert the chars to a String--- 
      String readString = 
       String.copyValueOf(inputBuffer, 0, charRead);      
      str += readString; 
      inputBuffer = new char[BUFFER_SIZE]; 
     } 
     in.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
     return ""; 
    }  
    return str;   
} 

但是,如果它需要時間來下載文本它顯示了一個空白屏幕,我想顯示一個進度條,而不是空白屏幕..我看到很多的進度條的例子,但沒有得到任何想法來執行這些在我的情況...請幫助我..

謝謝,

回答

1

使用AsyncTask,並把ProgressDialog在onPreExecute中,在doInBackground中執行下載,然後關閉onPostExecute中的對話框。

0
+0

感謝您的答覆jaydeep,我試圖實現http://stackoverflow.com/questions/5883537/issue-with-android-layout-和進度/ 5884128#5884128 – jiya 2011-05-06 09:34:45

+0

但得到了Syntex錯誤下面幾行:\t保護對象doInBackground(對象... PARAMS) \t { \t // TODO自動生成方法存根 \t //你的代碼 \t}令牌//語法錯誤「{」,你能不能幫我this..thanks – jiya 2011-05-06 09:40:29

+0

而不是複製它... 寫className類擴展的AsyncTask {} 然後 它會提示落實未實現的方法。 你會得到doInBackground()方法 ,請讓我知道你是否成功..如果它給錯誤..然後什麼是錯誤信息? – 2011-05-06 10:03:24

0

U可以在類中聲明的進展dialoge。 private ProgressDialog pd; 你可以在你的點擊事件中粘貼下面的代碼。 pd = ProgressDialog.show(context,「Please wait ...」,「Loading data ...」,true,false); 做完下載部分後寫 pd.dismiss();

0

做的過程中線程