2

I have been stuck in code where I wanted to change the adapter item image source of the active item. I have all set, still getting the active item position and invoking the method from service.

I have sendBroadcast method to send the intent with some data and receiving it in the fragment which holds RecyclerView.

And from there onReceive() method I invoke adapter method. And there comes the problem. In Adapter it's all running well till logging.

The problem is that the item needs a holder so that it can be changed, like

holder.imageview.setBackgroundResource(...);

but its not working even after notifyDatasetChanged(); please help me guys, below is my code. These are the methods from service that are invoked when needed:

 public void sendPlayBroadcast() {
    controlIntent.putExtra("control", "1");
    sendBroadcast(controlIntent);
    Log.e("sending broadcast","TRUE");
}

public void sendPauseBroadcast() {
    controlIntent.putExtra("control", "0");
    sendBroadcast(controlIntent);
    Log.e("sending broadcast","TRUE");
}

This is fragment, I receive the broadcast and invoke adapter methods:

BroadcastReceiver controlBR = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        sendControls(intent);
    }
};

private void sendControls(Intent cIntent) {
    String controls = cIntent.getExtras().getString("control");
    int control = Integer.parseInt(controls);
    switch (control) {
        case 1:
            mAdapter.Play_the_Icon();
            mAdapter.notifyDataSetChanged();
            Log.e("received broadcast","TRUE");
            break;
        case 0:
            mAdapter.Pause_the_Icon();
            mAdapter.notifyDataSetChanged();
            Log.e("received broadcast","TRUE");
            break;
    }
}

And these is the RecyclerView Adapter:

 public void Pause_the_Icon() {
    imageView.setImageResource(R.drawable.ic_play_arrow_24dp);
    Log.d("all good till here", "TRUE");
}

public void Play_the_Icon() {
    imageView.setImageResource(R.drawable.ic_equalizer_24dp);
    Log.d("all good till here", "TRUE");
}
Rami
  • 7,879
  • 12
  • 36
  • 66
Hussain ID
  • 231
  • 5
  • 12

1 Answers1

1

You need to update the image resource in onBindViewHolder() method, otherwise your changes will be lost.

Here is my suggestion:

1) Create an int variable in your adapter to refer to the resource file.

private int mImgResource;

2) Initialize your variable in your adapter constructor with a default value, eg:

mImgResource = R.drawable.ic_equalizer_24dp;

3) Change your Pause_the_Icon() and Play_the_Icon() methods to:

public void Pause_the_Icon() {
    mImgResource = R.drawable.ic_play_arrow_24dp;
    Log.d("all good till here", "TRUE");
}

public void Play_the_Icon() {
    mImgResource =R.drawable.ic_equalizer_24dp;
    Log.d("all good till here", "TRUE");
}

4) In your onBindViewHolder() method, do:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.imageview.setBackgroundResource(mImgResource);
}
Rami
  • 7,879
  • 12
  • 36
  • 66