这些问题有答案,但没有一个能解决我的问题.Below是我的代码。
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是否有任何其他方法可以刷新,或者是否有解决此问题的方法
答案 0 :(得分:0)
而不是
selectedPosition = holder.getAdapterPosition();
在对象类中维护一个标志,该标志将在特定复选框上设置。假设“已检查”是您在对象类中创建的标志,那么
on check change listener将其设置为true
if (isChecked) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
修改强>
if (quiz_g_sList.get(pos).isChecked) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
<强>更新强>
将您的对象类更改为此
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;
}
}
答案 1 :(得分:0)
我已经实现了这样,我得到了输出。
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();
}
}
});
答案 2 :(得分:0)
你需要做什么:
v-bind
. Customers::findOne(['account_number'=> Yii::$app->getSecurity()->encryptByKey($account_number, "someKeyHere")]);
)。 int selectedPosition
答案 3 :(得分:0)
尝试以下代码:
在Quiz_G_S
班级boolean isChecked
获取已选中CheckBox
selectedPosition = holder.getAdapterPosition();
重置List的所有其他对象,如下所示:
for (int i=0; i<list.size ; i++)
{
if (selectedPosition==i)
list.get(i).setIsChecked(true);
else
list.get(i).setIsChecked(false);
}
致电QuizAdapter.this.notifyDataSetChanged();
。
最后,选中如下所示的复选框:
if (list.get(position).getIsChecked())
{
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}