我们希望在单击列表视图中的项目时显示进度条。在 DetailActivity 同步发生并且进度条应该显示直到它完成。
下面给出的是包含列表项的 ListActivity 的代码。
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent intent = new Intent(ListActivity.this, DetailActivity.class);
intent.putExtra("position", position);
intent.putExtra("sk",(adapter.getSK(position)));
ListActivity.this.startActivityForResult(intent, REQUEST_DETAIL);
}
任何人都可以在这方面帮助我。
这是我们正在使用的progressdialog 的代码。
ProgressDialog progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Loading...");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setIndeterminate(true);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus=i;
// process some tasks
i=i+20;
// your computer is too fast, sleep 1 second
if(progressBarStatus==40)
{
// Synchronizing
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("the error is"+e);
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
对于此代码,将出现以下异常。
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.