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.