UjinUkr UjinUkr - 3 months ago 14
Android Question

Progress Bar does not work

Progress Bar does not work, it appears and disappears immediately

I want to progress bar is work all the time until a request is made, it works only for a moment.Explain to me , what it can be connected , I'm confused now!

Code

class MyAsyncTask extends AsyncTask<Void, Void, Void> {
ProgressBar loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = (ProgressBar) findViewById(R.id.progressBar);
loading.setVisibility(View.VISIBLE);
}

@Override
protected Void doInBackground(Void... params) {
VKRequest request = VKApi.audio().get();
request.executeWithListener(new VKRequest.VKRequestListener() {
@Override
public void onComplete(VKResponse response) {
super.onComplete(response);
list_music = (VKList) response.parsedModel;
list = new ArrayList<String>();
URLlist = new ArrayList<String>();
for (int i = 0; i < list_music.size(); i++) {
list.add(list_music.get(i).title + " - " + list_music.get(i).artist);
URLlist.add(list_music.get(i).url);
}
array = new ArrayAdapter<String>(VkMusicAct.this,
android.R.layout.simple_expandable_list_item_1, list);
listView.setAdapter(array);
}

@Override
public void onError(VKError error) {
super.onError(error);
Toast toastError = Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG);
toastError.show();
}
});
return null;
}

@Override
protected void onPostExecute(Void f) {
super.onPreExecute();
loading.setVisibility(View.GONE);
}

}

Answer

Use onProgressUpdate to dismiss the dialog. Override onProgressUpdate in your class as:

@Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
               loading.setVisibility(View.GONE);
        }

and in your doInBackground() use it as:

  @Override
  public void onComplete(VKResponse response) {
  super.onComplete(response);
  list_music = (VKList) response.parsedModel;
  list = new ArrayList<String>();
  URLlist = new ArrayList<String>();
     for (int i = 0; i < list_music.size(); i++) {
        list.add(list_music.get(i).title + " - " + list_music.get(i).artist);
      URLlist.add(list_music.get(i).url);
     }
                array = new ArrayAdapter<String>(VkMusicAct.this,
                android.R.layout.simple_expandable_list_item_1, list);
                listView.setAdapter(array);
                publishProgress();
            }