【发布时间】:2017-09-29 21:06:30
【问题描述】:
有这个问题的答案,但没有一个能解决我的问题。下面是我的代码。
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);
}
}
以上是我的适配器代码。
这是我的 Quiz_G_S 课
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;
}
}
我想要做的是,如果我选中一个复选框,那么所有复选框都将设置为未选中,除了我选中的那个 notifyDataSetChanged 有错误。我猜逻辑是正确的,但我不知道如何刷新复选框状态。我唯一知道的是 notifyDataSetChanged 是否有任何其他方法可以刷新或者是否有解决此问题的方法
【问题讨论】:
标签: android checkbox android-recyclerview