views:

40

answers:

1

I am using a progress bar in android, so that till my data is loaded, user is getting a proper feedback of what is going on. the code for the bar is below:

final ProgressDialog myProgressDialog;

myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true);
new Thread() {
   public void run() {
      try{
         setSelected();                     
         sleep(5000);
      } catch (Exception e) { }
      myProgressDialog.dismiss();
   }
}.start();

The setSelected() method is inserting data into an array list, and then creating an array adapter and then using a list view to show those items.

so when I put the setSelected() method in progress bar, it shows the loading circle, but once loading is done. Nothing is displayed in listview. But if I take out progress bar code (thread and run method), and simply call setSelected() method, the listview successfully declares the data. so far it concludes that the progress bar code is doing some thing...so wanted to know am I missing something to add in or take out from the code. I need to the show progress bar as I am reading data from online source and it takes time , which may make user uncomfortable.

+1  A: 

The Problem could be that you are trying to change the Userinterface from another thread then the UI thread, this is not possible in Android.

I would suggest to try the use of an Async Task. Asynct Task gives you the possibility to do something in a background thread and publish the results in the UI thread after it is done.

For this I would separate the setSelected Method in two Methods one that gets the data and creates the Array and everything else and one that creates the adapter and publishes the results to the list.

myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true);

final Result[] results = new Result[]();

new AsyncTask<Void, Void, Void>() {

    //This is done in the background
    protected Void doInBackground(Void... params) {
       results = getResults();
       return null;
    }

    //This id done in the UI Thread
    protected void onPostExecute(Long result) {
       publishResults();
       myProgressDialog.dismiss();
    }

   }
}.execute();
Janusz
Janusz!I used the method u told above but, the seek bar hangs on loading code is :new AsyncTask<Void, Void, Void>() {//This is done in the backgroundprotected Void doInBackground(Void... params) {setSelected();return null;}//This id done in the UI Threadprotected void onPostExecute(Long result) {populateList(data);myProgressDialog.dismiss();}}.execute();I guess the thread is dangling..is it??
kaibuki
sorry I need more code then this to really find an error here. Try to edit it in your original question
Janusz