How to give a progress bar in listview in android?
<p>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. </p>

<p>Given below is the code of our ListActivity which contains the list item.   </p>

<pre><code>listView.setOnItemClickListener(new OnItemClickListener()
  {
     public void onItemClick(AdapterView&lt;?&gt; 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);
}
</code></pre>

<p>Can anyone please help me in this.</p>

<p>This is the code for progressdialog that we are using.</p>

<pre><code>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 &lt; 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 &gt;= 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();
</code></pre>

<p>For this code the following exception is coming. </p>

<pre><code>android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
</code></pre>
