0

In Service class :

public void onNotify(TransferHandler<ProcessHolder> handler, int percentage) {
    updateprocess(percentage);
}

in adapter

onBindViewHolder

progressBar = new ProgressBar(getContext());
progressBar = parentView.findViewById(R.id.progressbar_process);

now I want to access percentage from service class method to this adapter progress

Sagar gujarati
  • 152
  • 1
  • 15
  • Check this answer: https://stackoverflow.com/a/37322906/6727154. And then look for the adapter method `notifyDataSetChanged`. – michaelitoh Aug 22 '18 at 11:58

2 Answers2

0

In the Service class I wrote this

public void onNotify(TransferHandler<ProcessHolder> handler, int percentage) {
    Intent intent = new Intent("PercentageUpdates");
    intent.putExtra("percentage", percentage);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

and at the Activity side you have to receive this Broadcast message

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
            mMessageReceiver, new IntentFilter("PercentageUpdates"));

By this way you can send percentage to an Activity. here mPercentageReceiver is the class in that class you will perform what ever you want....

private BroadcastReceiver mPercentageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String percentage = intent.getStringExtra("percentage");
        if (percentage != null) {
            // You can set the percentage here
        }
    }
};
0

Use EventBus library

After config that follow this steps

1 : Create servieClass.java

public class serviceClass {

private int percentage;

public serviceClass(int percentage) {
    this.percentage = percentage;
}

public int getPercentage() {
    return percentage;
}
}

2 : Change service

public void onNotify(TransferHandler<ProcessHolder> handler, int percentage) {
   updateprocess(percentage);
   EventBus.getDefault().post(new servieClass(percentage));
}

3 : add setPercentage function to your Adapter

public void setPercentage(int percentage){
    this.percentage = percentage;
    notifyDataSetChanged();
}

4 : Finally add this in fragment that you config EventBus in it

 @Subscribe
public void onEvent(BlockedEvent event) {
    adapter.setPercentage(percentage);
}

Good luck

Radesh
  • 13,084
  • 4
  • 51
  • 64