2010-06-05 65 views
0

在我的活動中,即時從網絡獲取一些大數據,並獲取這些數據時,我想向用戶顯示一個帶有旋轉輪的ProgressDialog。我只能將這段代碼放到一個線程中,對嗎? 問題是,即時通訊獲取這些數據後,我需要將它插入到TableLayout中作爲TableRows,並且似乎不可能從線程訪問TableLayout。在一個線程中查找TableLeyout(由於ProgressDialog)

我能做些什麼來顯示這個進度對話框並且能夠從線程訪問表格佈局? 線程結束時是否有任何事件發生?

我的代碼失敗了:

_tableLayout.addView(_tableRowVar, new TableLayout.LayoutParams(
     LayoutParams.FILL_PARENT, 
     LayoutParams.FILL_PARENT)); 

我完整的代碼:

final ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
      "Getting data.\nPlease wait...",true); 

new Thread() 
{ 
    public void run() 
    { 
     try 
     { 
      TableLayout _tableLayout; 
      _tableLayout = (TableLayout)MyActivity.this.findViewById(R.id.tableLayoutID); 

      List<String> data = getDataFromWeb(); 

      // Get the data and bind it into the table 
      publishTableLayoutWithTableRows(_tableLayout, data); 

     } 
     catch (Exception e) { 
      new AlertDialog.Builder(MyActivity.this) 
       .setMessage(e.getMessage()) 
      .show(); 
     } 
     dialog.dismiss(); 
    } 
}.start(); 

回答

0

您應該使用AsyncTask

你還會注意到評論can use UI thread here旁邊的方法,你可以使用UI。

+0

絕佳的作品! – Shaulian 2010-06-06 20:24:37

相關問題