0

嘿,我正在尝试让 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);
}
}

提前致谢

4

2 回答 2

2

阅读AsynTask,它专为这个用例而设计。

于 2011-01-07T14:00:15.713 回答
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);
于 2011-01-07T14:04:01.310 回答