[英]Running a Thread for Progress Bar View - Android
I have a sax parser which runs when the user taps a start button. 我有一个萨克斯解析器,当用户点击开始按钮时运行。 I would like to set a progress bar view which will update as the parser progresses. 我想设置一个进度条视图,该视图将随着解析器的进度而更新。
My code is currently as follows : 我的代码当前如下:
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressview);
try {
XMLParser.parse(getAssets().open("deindexed.xml"), this);
} catch (IOException e) {
Log.d("XML","onCreate(): parse() failed");
return;
}
//Start the Main Activity.
System.out.println("Activity recreated");
Intent i = new Intent(this, Simple.class);
startActivity(i);
}
The problem is that the setContentView does not update until the parser has completed (which I understand is to be expected). 问题在于,在解析器完成之前,setContentView不会更新(据我所知)。
Can anyone advise me on the best way to transform the above so it runs using a thread ? 谁能建议我转换上述内容的最佳方法,以便它可以使用线程运行?
Thank you. 谢谢。
Check out ASyncTasks. 查看ASyncTasks。 Or, use real Threading and use a Handler to pass messages back/forth. 或者,使用真实的线程并使用处理程序来回传递消息。 Using ASyncTasks is usually easier/neater, but sometimes it cannot be as versatile as a Thread. 使用ASyncTasks通常更容易/更整洁,但是有时它不能像Thread一样通用。 For your XML parser, an ASyncTask should work fine. 对于您的XML解析器,ASyncTask应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.