ListActivity和Dialogs

时间:2011-12-24 12:10:58

标签: java android dialog listactivity

我有ListActivity,需要通过Thread处理大量数据。我希望在Dialog中完成工作时显示background。但是,在视图加载完成之前,Dialog不会显示。我在常规activity上使用此方法,它可以正常工作(Dialog弹出,工作人员启动,工作人员完成,Dialog解雇。完成工作后,会向handler发送一条消息,以加载view并关闭Dialog

这是我的代码:

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle); 
    initViewProperties();
    dialogCode = 0;
    showDialog(dialogCode);

}

@Override
protected Dialog onCreateDialog(int id) {

    int resID = getResources().getIdentifier(getCfrFile(), "raw", getPackageName());        
    progDialog = new ProgressDialog(this);

    switch (dialogCode) {
        case 0:
            progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
            progDialog.setMessage(getCurrentTitle());
            break;
        case 1:
            progDialog.setMessage("Loading CFR");
            break;

        default:
            break;
    }       

    try {
        // do the work here
        xmlListRowFactoryHelper = new AcmListViewFactoryHelper(handler, resID, this, xpathBuilder(false), "title") ;
        xmlListRowFactoryHelper.run();

    } catch (Exception e) {

        e.printStackTrace();
    }

    return progDialog;

}   

final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

        int total = msg.getData().getInt("total");            

        progDialog.setProgress(total);

        if (total <= 0){

            xmlListRowFactoryHelper.setState(AcmTableRowFactoryHelper.DONE);
            // load the view here
            loadListView();    
        }
    }
}

2 个答案:

答案 0 :(得分:1)

使用AsyncTask,它很简单。 请参阅此处的示例和说明:http://developer.android.com/reference/android/os/AsyncTask.html

答案 1 :(得分:1)