1

I'm trying to load ListView, and I want that while the listview is loaded, a progress bar will be showed.

Firstly, this is my code (the result explanation will come after):

class MyTask extends AsyncTask<String,Integer,String>
{
final ListView listView = (ListView)findViewById(R.id.listView1);
final ProgressBar pb=(ProgressBar)findViewById(R.id.progressBar);


@Override
protected void onPreExecute()
{
    super.onPreExecute();
    pb.setVisibility(View.VISIBLE);
    listView.setVisibility(View.INVISIBLE);
}

@Override
protected String doInBackground(String...strings)
{
    populateEventList();

    return "done";
}

@Override
protected void onProgressUpdate(Integer...values)
{
    super.onProgressUpdate(values);
    pb.setProgress(values[0]);
    pb.setMax(values[1]);


}

@Override
protected void onPostExecute(String s)
{
    super.onPostExecute(s);

    listView.setVisibility(View.VISIBLE);
    pb.setVisibility(View.GONE);
}
}

The result is that the progress bar isn't showed anytime. The listview is loaded but the progress bar is not showed up.

If I'm removing the last line pb.setVisibility(View.GONE); , the progress line is showing up, but it isn't disappearing after the ListView is loaded.

What is wrong in my code?

Gilad1512
  • 21
  • 1
  • 4
  • Visit this link and check how `AsyncTask` works. http://stackoverflow.com/questions/18898039/using-asynctask – Stanojkovic Mar 28 '16 at 19:02
  • 1
    How are you calling your `AsyncTask`? You aren't using `.get()`, are you? – codeMagic Mar 28 '16 at 19:02
  • @codeMagic I'm calling like this: `new MyTask().execute();` – Gilad1512 Mar 28 '16 at 19:10
  • What is 'populateEventList()' doing? Can you edit to show that method? Maybe that is executing quickly so it looks like your `ProgressBar` is not showing. Also, you aren't passing anything to `onProgressUpdate()` so the values won't be updated. – codeMagic Mar 28 '16 at 19:36
  • Loading a listview likely isn't taking a lot of time, unless it has thousands of items, so it's probably getting shown so quickly that you aren't noticing it. And you need to call `publishProgress()` to actually update the ProgressBar and make it move. – NoChinDeluxe Mar 28 '16 at 19:41
  • @NoChinDeluxe The listView loads 5 items, and it shouldn't take so much time to be loaded. But when I try it, it does take like 2 seconds for the listView to be showed up. So why the progress bar isn't showed up until the ListView get loaded? – Gilad1512 Mar 28 '16 at 22:48
  • @codeMagic Hi please look at my comment above – Gilad1512 Mar 28 '16 at 22:48
  • I did. See my comment above. – codeMagic Mar 29 '16 at 00:16
  • @codeMagic Ok, so in my case what should I pass to `onProgressUpdate()`? – Gilad1512 Mar 29 '16 at 08:46
  • I don't see anything in your code that you would want to pass it. You don't ***need*** to use that function. In fact, I often don't. If you are, say, reading a file you could pass it the lines of the file to update the progress of the download. It's only really needed if it's a longer running operation so the user can see that it is progressing. Otherwise, showing a dialog until it finishes works well. – codeMagic Mar 31 '16 at 12:47

1 Answers1

0

Try using ProgressDialog instead of ProgressBar, this might be helpful: ProgressDialog in AsyncTask. I had difficulties on that as well, but once I changed to ProgressDialog, it was fairly simple to configure. Otherwise, I am suspecting that the method you call, populateEventList() takes no time. Try doing multiple ways, such as putting stuff inside thread in doInBackGround, to run in multi-thread, and so on.

Community
  • 1
  • 1
xosuma
  • 267
  • 1
  • 3
  • 11
  • Hi, thanks for your comment. I'm trying the ProgressDialog as you said, but I need a little bit help. Please if you don't mind to look at my code, and then tell me what I need to put in `onPostExecute()`? – Gilad1512 Mar 29 '16 at 09:02