Access TextView and Progressbar in Recyclerview from IntentService

270 views Asked by At

I am having a recyclerview which has a TextView (progresstxt) and a Progressbar (downloadprogress) as part of the inflated items.

I need to know how I can access the TextView and Progressbar in the SimpleAdapter from the IntentService so I can update their values?

I am trying to achieve this with a BroadcastReceiver but it doesn`t work, the TextView and Progressbar are not being updated.

Thank you in advance.

I access the TextView and Progressbar through onBindViewHolder.

public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> implements FollowRedirectsCallback {

private final Context mContext;
private String recievedStr;
private int receivedProg;

private MyBroadcastReceiver myBroadcastReceiver;
private MyBroadcastReceiver_Update myBroadcastReceiver_Update;

public static class SimpleViewHolder extends RecyclerView.ViewHolder {
        public final TextView title, progresstxt;
        public ProgressBar downloadprogress;

        public SimpleViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.simple_text);
            progresstxt = (TextView) view.findViewById(R.id.progress_text);
            downloadprogress = (ProgressBar) view.findViewById(R.id.progressdownload);
        }
    ...
public class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            recievedStr = intent.getStringExtra(MoService.EXTRA_KEY_UPDATE);
            //progresstxt.setText(result); ?
            //how to access the TextView in onBindViewHolder?
        }
    }

public class MyBroadcastReceiver_Update extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        receivedProg = intent.getIntExtra(MoService.EXTRA_KEY_UPDATE_PROGRESS, 0);
        //downloadprogress.setProgress(update); ?
        //how to access the Progressbar in onBindViewHolder?

    }
}

...

@Override
    public void onBindViewHolder(final SimpleViewHolder holder, final int position) {
      //
        myBroadcastReceiver = new MyBroadcastReceiver();
        myBroadcastReceiver_Update = new MyBroadcastReceiver_Update();

        //register BroadcastReceiver
        IntentFilter intentFilter = new IntentFilter(MoService.ACTION_MyIntentService);
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        mContext.registerReceiver(myBroadcastReceiver, intentFilter);

        IntentFilter intentFilter_update = new IntentFilter(MoService.ACTION_MyUpdate);
        intentFilter_update.addCategory(Intent.CATEGORY_DEFAULT);
        mContext.registerReceiver(myBroadcastReceiver_Update, intentFilter_update);

      //
        holder.progresstxt.setText(recievedStr);
        holder.downloadprogress.setProgress(receivedProg);
        holder.progresstxt.setVisibility(View.VISIBLE);
        holder.downloadprogress.setVisibility(View.VISIBLE);

      //
    }

    mContext.unregisterReceiver(myBroadcastReceiver);
    mContext.unregisterReceiver(myBroadcastReceiver_Update);
}

Through a IntentService (MoService) several file operations are done and I want to access/update the TextView and Progressbar from this IntentService.

public class MoService extends IntentService {

public static final String ACTION_MyIntentService = "com.sample.RESPONSE";
    public static final String ACTION_MyUpdate = "com.sample.UPDATE";
    public static final String EXTRA_KEY_UPDATE = "EXTRA_UPDATE";
    public static final String EXTRA_KEY_UPDATE_PROGRESS = "EXTRA_UPDATE_PROGRESS";
    String message;
    int mProgress;
..
public MoService() {
        super("MyService");
    }

    @Override
    public void onCreate() {
        context = this;
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    //
        Intent intentUpdate = new Intent();
        intentUpdate.setAction(ACTION_MyUpdate);
        intentUpdate.addCategory(Intent.CATEGORY_DEFAULT);
        intentUpdate.putExtra(EXTRA_KEY_UPDATE, message);
        sendBroadcast(intentUpdate);
    //
    }

class MyDownloadDownloadStatusListener implements DownloadStatusListener {
        @Override
        public void onDownloadComplete(DownloadRequest request) {
           message = "Status: completed...";
           mProgress = 0;
           progresstxt.setVisibility(View.GONE);
           downloadprogress.setVisibility(View.GONE);
..
  }
}
1

There are 1 answers

2
Aditya K On

There are a couple of issues with the code. You should not create a new Receiver in your onBindViewHolder method. There should be a single receiver which updates the Item list used by your adapter and call notifyDataSetChanged on the adapter.

Hope it helps