问题
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();
}
}
});
回答1:
Your implementation is wrong.
You should only use the onCreateViewHolder()
to inflate layout.
The binding operations should be in onBindViewHolder()
. Use toggle operations in onBindViewHolder()
回答2:
I done a quick edit. Please check in detail. I used position
in SharedPreferences
. So each toggle button
value save in different key. This may work.
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
@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));
editor.putBoolean("abc"+position, true);
editor.commit();
}
else{
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
editor.putBoolean("abc"+position, false);
editor.commit();
}
}
});
回答3:
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();
}
}
});
来源:https://stackoverflow.com/questions/45979628/toggle-button-positions-in-recyclerview