Omar Hassan
Omar Hassan

Reputation: 747

Android AsyncTask and progress bars

I have a problem in My android app, I searched too much but i couldn't find a clue for solving it. this is my problem

I have a list view , onclick on each of the list items i start a new AsyncTask which update the a progress bar in the list item.

the problem comes when the system destroys my activity for any reasons, the AsyncTasks remain working but it doesn't affect the newly created progress bars,

any one can help me ? Thanks in advance.

Upvotes: 0

Views: 492

Answers (3)

Abhishek Birdawade
Abhishek Birdawade

Reputation: 301

I was suffering from the same problem then what i have done is i was having a Serializable class in which data for the listview is stored and in that class i created a variable named

int progress;

When anything gets updated from asynsTask i set the updates to this variable which is unique for each row because of serializable class and object.Here is the example -:

Content.java

 public class Content implements Serializable {

            @SerializedName("id")
            protected String id;

            protected int progress = -1;

            public String getId() {
                   return id;
            }

            public String setId(String id) {
                   this.id = id;
            }

            public int getProgress() {
                   return progress;
            }

            public void setProgress(int progress) {
                   this.progress = progress;
            }
    }

MainActivity.java -:

AsyncAdapter -:

    public class AsyncAdapter extends ArrayAdapter<Content>   {

    Context context;
    ArrayList<Content> items;
    private static final int TYPE_MAX_COUNT = 1;

    public AsyncAdapter(Context context, int resource,
            int textViewResourceId, ArrayList<Content> items) {
        super(context, resource, textViewResourceId, items);
        this.context = context;
        this.items = items;
    }

    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

                 // Layout inflate code

                 // if else condition to visible progress bar
                 viewHolder.progressbar.setProgress(content.getProgress(position));

                 viewHolder.downloadButton
                        .setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                 //Start service here
                 Intent serviceIntent = new Intent(MainActivity.this,DownloadService.class);
                 serviceIntent.putExtra("id",id);
                 startService(serviceIntent);

            }});
    }
 }

In your BroadCastReceiver, you can set the progress and can update progress of progressbar.

public class DownloadReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, final Intent intent) {

    final int progress = intent.getIntExtra("progress", 0);
    final String broadcastId = intent.getStringExtra("id"); 

  for(int i=0;i<contents.size();i++) {
        if(contents.getId().equals(broadcastId)) {
             contents.get(i).setProgress(progress);
        }
   }

   adapter.notifyDataSetChanged(); // to update progressbar and listview
  removeStickyBroadcast(intent);
 }

And from service you can send the progress to receiver and can update it simultaneously.I think this is the better option instead of using asynTask.To add data in Content you will either have parse it and add using gson or add it explicitely.I hope this helps you and you will get better idea how to handle progressbars within listview.

Upvotes: 1

Syamantak Basu
Syamantak Basu

Reputation: 935

I think this Post can help you. But this is particularly for that case where your activity is recreated due to Configuration change.

Upvotes: 0

kirankk
kirankk

Reputation: 628

Create one callback listener method and override it in your activity.Call this callback method from onProgressUpdate in AsynkTask.and update the progress bar.

Upvotes: 0

Related Questions