Reputation: 162
There are answers of this questions but none of them solved my problem .Below is my code .
public class QuizAdapter extends RecyclerView.Adapter<QuizAdapter.MyViewHolder> {
Context context;
private List<Quiz_G_S> quiz_g_sList = null;
int selectedPosition = -1;
public QuizAdapter(Context context, List<Quiz_G_S> list) {
this.context = context;
this.quiz_g_sList = list;
}
@Override
public QuizAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quiz_single_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final QuizAdapter.MyViewHolder holder, int position) {
final int pos = position;
holder.answers.setText(quiz_g_sList.get(pos).getMCQ());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
quiz_g_sList.get(pos).setChecked(true);
}
});
if (quiz_g_sList.get(pos).isChecked()) {
holder.checkBox.setChecked(true);
notifyDataSetChanged();
} else {
holder.checkBox.setChecked(false);
}
}
@Override
public int getItemCount() {
return quiz_g_sList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView answers;
CheckBox checkBox;
public MyViewHolder(View v) {
super(v);
answers = (TextView) v.findViewById(R.id.quiz_adap_ans);
checkBox = (CheckBox) v.findViewById(R.id.quiz_adap_check);
}
}
Above is my adapter code.
This is my Quiz_G_S class
public class Quiz_G_S {
String MCQ;
int Rank;
boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getMCQ() {
return MCQ;
}
public void setMCQ(String mcq) {
this.MCQ = mcq;
}
public int getRank() {
return Rank;
}
public void setRank(int rank) {
this.Rank = rank;
}
}
What i want to do is if i check a checkbox then all checkboxes will set unchecked except the one I checked There is an error on notifyDataSetChanged. I guess the logic is right but i don't know how to refresh checkbox state.The only thing I know is notifyDataSetChanged is there any other method to refresh or is there any workaround for this problem
Upvotes: 0
Views: 3503
Reputation: 440
try below code:
Define one more Object in your Quiz_G_S
Class boolean isChecked
Get the position of checked CheckBox
selectedPosition = holder.getAdapterPosition();
Reset all other objects of List like below:
for (int i=0; i<list.size ; i++)
{
if (selectedPosition==i)
list.get(i).setIsChecked(true);
else
list.get(i).setIsChecked(false);
}
call QuizAdapter.this.notifyDataSetChanged();
.
In last, check selected check box like below:
if (list.get(position).getIsChecked())
{
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
Upvotes: 1
Reputation: 15046
What do you have to do:
int selectedPosition
).if (position != selectedPosition) {
// user selected another item so previous one should be updated
adapter.notifyItemChanged(selectedPosition);
selectedPosition = position;
}
Upvotes: 0
Reputation: 1275
Instead of
selectedPosition = holder.getAdapterPosition();
maintain a flag inside your object class which will be set on Check of particular checkbox suppose "checked" is your flag that you have created in your object class then
on check change listener set it to true
if (isChecked) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
EDIT
if (quiz_g_sList.get(pos).isChecked) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
UPDATE
change your object class to this
public class Quiz_G_S {
String MCQ;
int Rank;
boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getMCQ() {
return MCQ;
}
public void setMCQ(String mcq) {
this.MCQ = mcq;
}
public int getRank() {
return Rank;
}
public void setRank(int rank) {
this.Rank = rank;
}
}
Upvotes: 0
Reputation: 1219
I have implemented like this, where i got output.
if(view == null) {
view = inflater.inflate(R.layout.todo_checkbox_adapter_layout, parent, false);
holder = new ViewHolder();
holder.todoItemTextView = (TextView) view.findViewById(R.id.item_tv);
holder.checkedTextView = (CheckBox) view.findViewById(R.id._item_checkbox);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.todoItemTextView.setTag("" + position);
holder.checkedTextView.setChecked(displayModel.isFinished());
holder.checkedTextView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
try {
String tag = (String) holder.todoItemTextView.getTag();
int pos = Integer.parseInt(tag);
todoItemBeanList.get(pos).setFinished(isChecked);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
});
Upvotes: 0