1

I hava a ListView and when you click on each row it starts downloading from a service in a new Intent.

How can I update the progressBar in the clicked row from the information that I get from service?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
DavidOli
  • 75
  • 2
  • 8

3 Answers3

1

In service send broadcast like below

Intent i = new Intent("Updated_Count");  
i.putExtra("progress", <progress count>); 
i.putExtra("row_index", <row_index>);  
sendBroadcast(i);

On other side in activity receive it

public class newMessage extends BroadcastReceiver 
{
     @Override
     public void onReceive(Context context, Intent intent) 
     {    
        String action = intent.getAction();
        if(action.equalsIgnoreCase("Updated_Count")){    
        Bundle extra = intent.getExtras();
        String progress= extra.getString("progress"); 
        String index = extra.getString("row_index");  
         // now you can play with progress for any particular list row         
     }
}

You can place above class as sub class in to your list activity

Don't forget to register/unregister broadcast receiver

Happy Coding!

Shrenik Shah
  • 1,900
  • 1
  • 11
  • 19
  • if you found it helpful to you and it resolves what you want kindly mark this as answer so that it can be useful to others as well. – Shrenik Shah Aug 03 '16 at 10:21
  • each row of my listview has their own progressBar , so how can I exactly update the exact row's progressBar ? – DavidOli Aug 03 '16 at 10:41
  • that you have to manage at service end, if there is multiple downloads on going than pass index or id or something unique thing for each row from service via put extra – Shrenik Shah Aug 03 '16 at 10:44
  • when i want to pass the position of the row of listview as the unique id , inside the onclick of the row , it doesnt give me the right position , for example for two rows it returns the same id , like 1 or 0 – DavidOli Aug 03 '16 at 11:19
  • that is separate thread, we can't mix up things here otherwise it won't helpful to anyone – Shrenik Shah Aug 03 '16 at 11:25
  • you can search for that or ask as a new question – Shrenik Shah Aug 03 '16 at 11:25
  • You can use URL to distinguish list items. Details refer to https://github.com/li2/DownloadDemo – Weiyi Aug 10 '16 at 03:07
0

Use a broadcast receiver to notify the Progress bar in the activity. In your case the broadcast on receive you will update the progress bar and then notify the adapter of the data change.

Santa Teclado
  • 186
  • 10
0

You can use Broadcast Receiver from service to the activity or fragment.you have to register and unregistered broadcast on onResume() and on onPause() and created Broadcast instance in activity and sendBroadcast through service you can refer the following example: http://www.truiton.com/2014/09/android-service-broadcastreceiver-example/

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51