I have toggle buttons inside recycler view i'm saving the boolean value in sharedpreference to recognize if the toggle button is pressed or not on every restart of application now the problem is when i click on a 1 toggle button and close the application every toggle button get on same thing for off button there is no position connection between the toggle button and recycler view here is my code
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder holder=new MyHolder(v);
SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE);
Boolean a = sharedPrefs.getBoolean("abc" , false);
if(a) {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light));
holder.fav.setChecked(true);
} else {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
holder.fav.setChecked(false);
}
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.nameTxt.setText(players[position]);
holder.posTxt.setText(positions[position]);
holder.img.setImageResource(images[position]);
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc", true);
editor.commit();
} else {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc", false);
editor.commit();
}
}
});
it worked thank you all this is the right answer
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder holder=new MyHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.nameTxt.setText(players[position]);
holder.posTxt.setText(positions[position]);
holder.img.setImageResource(images[position]);
SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE);
Boolean a = sharedPrefs.getBoolean("abc" + position, false);
if (a){
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light));
holder.fav.setChecked(true);
}else {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
holder.fav.setChecked(false);
}
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc" + position, true);
editor.commit();
}
else{
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc" + position, false);
editor.commit();
}
}
});