获取 XML 后显示 ProgressDialog
嘿, 我试图在从网络获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读 AsynTask,它就是为这个用例而设计的。
Read up on AsynTask, it is designed for just this use-case.
要创建进度对话框,您应该在 Activity 中覆盖“onCreateDialog”:
并在加载 xml 数据之前使用“showDialog”显示它:
并使用“removedDialog”删除它:
To create a progress dialog, you should override "onCreateDialog" in your Activity:
and display it using "showDialog" before loading the xml data:
and "removedDialog" to get rid of it: