2011-02-18 91 views
0

我有一個ListActivity類,當點擊列表中的任何項目時,會顯示一個新的活動。新的活動需要時間來加載,所以我希望用戶知道有事情發生(在進度對話框的形式)Android - 進度對話框不關閉

所以,爲了做到這一點,我實現Runnable接口在我的班級像這樣 -

public class ProtocolListActivity extends ListActivity implements Runnable { 
private ProgressDialog progDialog; 
.... 
protected void onListItemClick(ListView l, View v, int position, long id) { 
        progDialog.show(this, "Showing Data..", "please wait", true, false); 

    Thread thread = new Thread(this); 
    thread.start(); 
} 
.... 
public void run() { 
    // some code to start new activity based on which item the user has clicked. 
} 

最初,當我點擊,並且正在加載的新的活動,進度對話框工作得很好,但是當我關閉之前的活動(要回這個名單)進度對話框仍在運行。我希望進度對話框僅在新活動開始時顯示。

有人可以請指導我如何正確地做到這一點。

回答

3

對話框需要由程序員明確刪除(或由用戶關閉)。所以,應該這樣做:

在活動A(呼叫活動)

protected void onListItemClick(ListView l, View v, int position, long id) { 
    progDialog.show(this, "Showing Data..", "please wait", true, false); 

    Thread thread = new Thread(this){ 
     // Do heavy weight work 

     // Activity prepared to fire 

     progDialog.dismiss(); 
    }; 
    thread.start(); 
} 

雖然在大多數情況下使用,繁重的工作應在被叫活動。在情況下,繁重的工作是做被叫的onCreate,它應該是這樣的:

活動B(被叫):

onCreate(){ 
    progDialog.show(this, "Showing Data..", "please wait", true, false); 

    Thread thread = new Thread(this){ 
     // Do heavy weight work 

     // UI ready 

     progDialog.dismiss(); 
    }; 
    thread.start(); 
} 

不管怎麼說,這個想法仍然是相同的。