0

I want to use spinning progress bar in asynctask class. Below is my asynctask class code.

Before i use progress dialog which works properly.

public class Downloader extends AsyncTask<Void,Void,Object> {


    Context c;
    String urlAddress;
    RecyclerView rv;
public Downloader(Context c, String urlAddress, RecyclerView rv) {
    this.c = c;
    this.urlAddress = urlAddress;
    this.rv = rv;
    }


@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Object doInBackground(Void... voids) {
    return this.downloadData();
}

@Override
protected void onPostExecute(Object data) {
    super.onPostExecute(data);

    if(data.toString().startsWith("Error"))
    {
        Toast.makeText(c,data.toString(),Toast.LENGTH_SHORT).show();
    }else new RssParser(c, (InputStream) data, rv).execute();
}

private Object downloadData()
{
    Object connection=Connector.connect(urlAddress);
    if(connection.toString().startsWith("Error"))
    {
        return connection.toString();
    }

Please comment if you want any other information.

Harsh Tiwari
  • 55
  • 1
  • 6

2 Answers2

0

If you want any customization for showing the progress bar in asycTask you can refer following link. Android custom progress indicator while doing AsyncTask

Sagar
  • 1
  • 1
0

For my projects somtimes I use this method:

I attached ProgressBar into my layout, but I did it invisible. And in code I write this steps:

private class ParseTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {

       ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setVisibility(ProgressBar.VISIBLE);

            //My code
            }




@Override
protected void onPostExecute(String strJson) {

    //My code

    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(ProgressBar.INVISIBLE);



}

}

So, I "turn on" ProgressBar in doInBackground and "turn off" it in onPostExecute

Valentin
  • 400
  • 2
  • 11