We want to show a progress bar on click of an item in listview. In DetailActivity synchronization takes place and the progress bar should be shown untill it completes.
Given below is the code of our ListActivity which contains the list item.
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);
}
Can anyone please help me in this.
This is the code for progressdialog that we are using.
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();
For this code the following exception is coming.
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.