2011-12-13 83 views
0

我正在進行延遲加載並幾乎完成了它。但是想要實現一個進度對話框,因爲在開始活動和完成顯示內容之間需要大約10秒。一旦我點擊一個按鈕開始,它將停留在當前頁面(Main.java)約4秒鐘,然後移動到下一頁面(Activity.java)。然後顯示內容大約需要2-4秒鐘。Android - 單擊按鈕後右鍵單擊顯示進度對話框

嘗試了這裏和網上可用的例子,但它們工作不正常(能夠顯示對話框,但在內容全部下載後無法做適當的解僱)。

問題是,一旦用戶點擊按鈕,我該如何立即實現一個進度指示器?

Activity.java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   

    list=(ListView)findViewById(R.id.list);   
    adapter=new LazyAdapter(this, mStrings, dStrings); 
    list.setAdapter(adapter); 
}  

private String[] mStrings = {}; 
private String[] dStrings = {}; 

public Activity() 
{ 
    String imageC = ""; 
    String textC = ""; 

    try { 
     // Get the URL from text box and make the URL object 

     URL url = new URL(targetURL); 

     // Make the connection 
     URLConnection conn = url.openConnection(); 
     BufferedReader reader = new BufferedReader(
     new InputStreamReader(conn.getInputStream())); 

     String line = reader.readLine(); 
     Pattern sChar = Pattern.compile("&.*?;"); 
     Matcher msChar = sChar.matcher(line); 
     while (msChar.find()) line = msChar.replaceAll(""); 

     while (line != null) { 

      if(line.contains("../../")) 
      { 

       int startIndex = line.indexOf("../../") + 6; 
       int endIndex = line.indexOf(">", startIndex + 1); 
       String abc = "http://www.petschannel.com/"; 
       String imageSrc = line.substring(startIndex,endIndex); 
       //complete full url 
       String xyz = abc +imageSrc; 
       xyz = xyz.substring(0,xyz.indexOf('"')); 
       xyz = xyz +";"; 
       imageC += xyz;     
       mStrings = imageC.split(";"); 
       line = reader.readLine(); 
      } 

      if(line.contains("../../") == false) 
      { 
       line = reader.readLine(); 
      } 

      if (line.contains("Gnametag")) 
      { 
       int startIndex = line.indexOf("Gnametag") + 10; 
       int endIndex = line.indexOf("<", startIndex + 1); 
       String gname = line.substring(startIndex,endIndex); 
       textC += "Name: "+gname+ "\n"; 
      }    

      if (line.contains("Last Update")) 
      { 
       reader.close(); 
      }        
     }   

     // Close the reader 
     reader.close(); 

    } catch (Exception ex) { 
     ex.printStackTrace();   
    } 
}  

回答

0

首先,你在主線程上做你的網絡呼叫,這是一個典型的沒有沒有因爲性能原因,除其他。切勿在主(ui)線程上執行任何可能耗費時間的操作。

我建議使用AsyncTask,它確保在這種情況下,您的網絡調用將在工作線程中完成,並且結果返回到主線程。

AsyncTask有管理進度條的方法,onProgressUpdatepublishProgress這將幫助您解決您陳述的問題。有很多關於此的好文章,here is one

+0

那麼我的情況建議什麼?我嘗試創建一個私人字符串[]。然後將方法更改爲void。但它沒有運行該方法。我需要通過使用AsyncTask來完成什麼?打電話給我的方法?謝謝 – Hend

0

事情是這樣的:

final ProgressDialog myDialog = ProgressDialog.show(this, "Title", "Message"); 

Thread thread = new Thread(new Runnable() { 
    public void run() { 
     /* Your code goes here */ 
     mHandler.sendEmptyMessage(0); 
    } 

    private Handler mHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      myDialog.dismiss(); 
     } 
    } 
} 

thread.start();