开发者

Using progress bar ends up in not populating list view

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开发者_JS百科 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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜