自动检查或取消选中具有布尔值的RecyclerView复选框

问题描述:

我需要根据来自POJO类的布尔值自动检查我的recyclerview复选框。以下是我的课程细节。自动检查或取消选中具有布尔值的RecyclerView复选框

HealthStatusCheck.java

public class HealthStatusCheck { 
private int id; 
private int donorID; 
private boolean q1; 
private boolean q2; 
private boolean q3; 
private boolean q4; 
private boolean q5; 
private boolean q6; 
private boolean q7; 
private boolean q8; 
private boolean q9; 
private boolean q10; 
private boolean q11; 
private boolean q12; 
private boolean q13; 
private boolean q14; 
private boolean q15; 
private boolean q16; 
private boolean q17; 
private boolean q18; 
private boolean q19; 
private boolean q20; 

public HealthStatusCheck() {} 

public HealthStatusCheck(int id, int donorID, boolean q1, boolean q2, boolean q3, boolean q4, boolean q5, boolean q6, 
         boolean q7, boolean q8, boolean q9, boolean q10, boolean q11, boolean q12, boolean q13, 
         boolean q14, boolean q15, boolean q16, boolean q17, boolean q18, boolean q19, boolean q20) { 
    this.id = id; 
    this.donorID = donorID; 
    this.q1 = q1; 
    this.q2 = q2; 
    this.q3 = q3; 
    this.q4 = q4; 
    this.q5 = q5; 
    this.q6 = q6; 
    this.q7 = q7; 
    this.q8 = q8; 
    this.q9 = q9; 
    this.q10 = q10; 
    this.q11 = q11; 
    this.q12 = q12; 
    this.q13 = q13; 
    this.q14 = q14; 
    this.q15 = q15; 
    this.q16 = q16; 
    this.q17 = q17; 
    this.q18 = q18; 
    this.q19 = q19; 
    this.q20 = q20; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public int getDonorID() { 
    return donorID; 
} 

public void setDonorID(int donorID) { 
    this.donorID = donorID; 
} 

public boolean isQ1() { 
    return q1; 
} 

public void setQ1(boolean q1) { 
    this.q1 = q1; 
} 

public boolean isQ2() { 
    return q2; 
} 

public void setQ2(boolean q2) { 
    this.q2 = q2; 
} 

public boolean isQ3() { 
    return q3; 
} 

public void setQ3(boolean q3) { 
    this.q3 = q3; 
} 

public boolean isQ4() { 
    return q4; 
} 

public void setQ4(boolean q4) { 
    this.q4 = q4; 
} 

public boolean isQ5() { 
    return q5; 
} 

public void setQ5(boolean q5) { 
    this.q5 = q5; 
} 

public boolean isQ6() { 
    return q6; 
} 

public void setQ6(boolean q6) { 
    this.q6 = q6; 
} 

public boolean isQ7() { 
    return q7; 
} 

public void setQ7(boolean q7) { 
    this.q7 = q7; 
} 

public boolean isQ8() { 
    return q8; 
} 

public void setQ8(boolean q8) { 
    this.q8 = q8; 
} 

public boolean isQ9() { 
    return q9; 
} 

public void setQ9(boolean q9) { 
    this.q9 = q9; 
} 

public boolean isQ10() { 
    return q10; 
} 

public void setQ10(boolean q10) { 
    this.q10 = q10; 
} 

public boolean isQ11() { 
    return q11; 
} 

public void setQ11(boolean q11) { 
    this.q11 = q11; 
} 

public boolean isQ12() { 
    return q12; 
} 

public void setQ12(boolean q12) { 
    this.q12 = q12; 
} 

public boolean isQ13() { 
    return q13; 
} 

public void setQ13(boolean q13) { 
    this.q13 = q13; 
} 

public boolean isQ14() { 
    return q14; 
} 

public void setQ14(boolean q14) { 
    this.q14 = q14; 
} 

public boolean isQ15() { 
    return q15; 
} 

public void setQ15(boolean q15) { 
    this.q15 = q15; 
} 

public boolean isQ16() { 
    return q16; 
} 

public void setQ16(boolean q16) { 
    this.q16 = q16; 
} 

public boolean isQ17() { 
    return q17; 
} 

public void setQ17(boolean q17) { 
    this.q17 = q17; 
} 

public boolean isQ18() { 
    return q18; 
} 

public void setQ18(boolean q18) { 
    this.q18 = q18; 
} 

public boolean isQ19() { 
    return q19; 
} 

public void setQ19(boolean q19) { 
    this.q19 = q19; 
} 

public boolean isQ20() { 
    return q20; 
} 

public void setQ20(boolean q20) { 
    this.q20 = q20; 
} 
} 

以上就是我的POJO类。现在我需要根据分配给POJO类的布尔值设置recyclerview复选框。在我的recyclerview适配器中,此POJO类可用。

这里是我的适配器代码:

public class DonorHealthStatusAdapter extends RecyclerView.Adapter<DonorHealthStatusAdapter.HealthViewHolder> { 

private Context context; 
private ArrayList<DonorHealthStatusQuestionare> questionares; 
private HealthStatusCheck healthStatusCheck; 


public DonorHealthStatusAdapter(Context context, ArrayList<DonorHealthStatusQuestionare> questionares, 
           HealthStatusCheck healthStatusCheck) { 
    this.context = context; 
    this.questionares = questionares; 
    this.healthStatusCheck = healthStatusCheck; 
} 

@Override 
public HealthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_donor_health_info, parent, false); 
    return new HealthViewHolder(view); 
} 

@Override 
public void onBindViewHolder(final HealthViewHolder holder, int position) { 

    DonorHealthStatusQuestionare questionare = questionares.get(position); 
    holder.tv_questions.setText(questionare.getQuestions()); 

    holder.cbYes.setOnCheckedChangeListener(null); 
    holder.cbNo.setOnCheckedChangeListener(null); 



} 

@Override 
public int getItemCount() { 
    return questionares.size(); 
} 

public class HealthViewHolder extends RecyclerView.ViewHolder { 

    CardView cardView; 
    TextView tv_questions; 
    CheckBox cbYes, cbNo; 

    public HealthViewHolder(View itemView) { 
     super(itemView); 
     cardView = itemView.findViewById(R.id.card_view_health_status_rv); 
     tv_questions = itemView.findViewById(R.id.tv_eligibility_question); 
     cbYes = itemView.findViewById(R.id.checkBoxYes); 
     cbNo = itemView.findViewById(R.id.checkBoxNo); 
    } 

} 

} 
+0

那么是什么问题?你有布尔人......你有复选框。还需要什么? – Wizard

+0

我需要在回收视图中根据布尔值设置选择。回收利用者视图中有20行。每行有复选框,我需要选择或不选择。 –

+0

每20行有20个不同的值。从我的适配器中选择设置后,所有复选框都处于选中状态。 –

得到了你的观点。

两个选择 -

1)创建另一个ArrayList<HealthStatusCheck> answers,并根据其填充行。
2)对webserivce进行更改,并在Json格式的问题对象内得到问题的答案。

+0

让我试试这个。 –

+0

第一个选项不会工作。因为只有一个可容纳20个布尔值的健康状态检查对象。 webservice无法返回20 healthstatuscheck对象。它只返回一个具有20个布尔字段的对象。我认为我们不能使用对象字段来填充recyclerview。 –

+0

思考我应该在webservice中做什么修改。如果您有任何想法或数据库设计策略,请与我分享。 –

所以你有20个问题,这意味着你有20行,在每一行中有两个复选框(根据你的布尔类的类模型,它们应该是或不是)。

在绑定观点持有者

做到这一点:

@Override 
    public void onBindViewHolder(final HealthViewHolder holder, int position) { 

DonorHealthStatusQuestionare questionare = questionares.get(position); 
holder.tv_questions.setText(questionare.getQuestions()); 

//for question 1 

    holder.cbYes.setChecked(questionare.isQ1()); 
    holder.cbNo.setChecked(questionare.isQ1()); 

    //for question 2 

    holder.cbYes.setChecked(questionare.isQ2()); 
    holder.cbNo.setChecked(questionare.isQ2()); 


    //for question 3 
    holder.cbYes.setChecked(questionare.isQ3()); 
    holder.cbNo.setChecked(questionare.isQ3()); 

    //and so on till you reach the last question (20 in your case) 



    } 
+0

当第一次检查分配集时,回收站视图内的所有复选框都将被选中。 –

+0

数据源的结构是什么。 –