获取 XML 后显示 ProgressDialog

发布于 2024-10-10 20:13:31 字数 1619 浏览 3 评论 0原文

嘿, 我试图在从网络获取 XML 的同时让 ProgressDialog 正常工作。问题在于,一旦加载并显示 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);
}
}

提前致谢

Hey,
I'm trying to get a ProgressDialog working while im fetching a XML from the web. The problem is that the dialog shows up as soon as the XML is loaded and displayed. If I dismiss it afterwards it won't show up at all. Any ideas what to do? I'm new to android development and this is my first app, I've already searched the net but didn't find any working solution..
Here is the shortened code:

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);
}
}

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-10-17 20:13:31

阅读 AsynTask,它就是为这个用例而设计的。

Read up on AsynTask, it is designed for just this use-case.

鹿港小镇 2024-10-17 20:13:31

要创建进度对话框,您应该在 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);

To create a progress dialog, you should override "onCreateDialog" in your Activity:

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

and display it using "showDialog" before loading the xml data:

showDialog(0);

and "removedDialog" to get rid of it:

context.removeDialog(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文