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");
}