获取XML后显示ProgressDialog

时间:2011-01-07 13:54:53

标签: android xml progressdialog

嘿 我正在尝试使用ProgressDialog工作,同时我从网上获取XML。问题是,只要加载并显示XML,对话框就会显示出来。如果我事后解雇它根本就不会出现。有什么想法怎么办?我是Android开发的新手,这是我的第一个应用程序,我已经搜索过网络,但没有找到任何可行的解决方案.. 这是缩短的代码:

public class showReleases extends Activity {
    SitesList sitesList = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.releasedetails);
    ProgressDialog dialog = new ProgressDialog(showReleases.this);
    dialog.setMessage("please wait");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.show();

    getFeed2();

    dialog.dismiss();
}


public void getFeed2() {
    Bundle extras = getIntent().getExtras();

    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        URL sourceUrl = new URL("http://www.it-leaked.com/app/details.php?id=" + extras.getString("id"));

        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    sitesList = MyXMLHandler.sitesList;

    TextView txtView = (TextView)findViewById(R.id.TextView01);
    txtView.setText(sitesList.getTitle().get(0));
    TextView txtView2 = (TextView)findViewById(R.id.TextView02);
    String myTracklist = sitesList.getTracklist().get(0);
    myTracklist = myTracklist.replace("||||", "\n");
    txtView2.setText(myTracklist);
}
}

提前致谢

2 个答案:

答案 0 :(得分:2)

阅读AsynTask,它仅适用于此用例。

答案 1 :(得分:0)

要创建进度对话框,您应该在Activity中覆盖“onCreateDialog”:

@Override
protected Dialog onCreateDialog(int id) {
    ProgressDialog dialog = new ProgressDialog(this);
    dialog.setMessage("loading...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    return dialog;
}

并在加载xml数据之前使用“showDialog”显示它:

showDialog(0);

和“removedDialog”摆脱它:

context.removeDialog(0);