2017-11-18 127 views
-1

我需要根據來自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

得到了你的觀點。

兩個選擇 -

1)創建另一個ArrayList<HealthStatusCheck> answers,並根據其填充行。
2)對webserivce進行更改,並在Json格式的問題對象內得到問題的答案。

+0

讓我試試這個。 –

+0

第一個選項不會工作。因爲只有一個可容納20個布爾值的健康狀態檢查對象。 webservice無法返回20 healthstatuscheck對象。它只返回一個具有20個布爾字段的對象。我認爲我們不能使用對象字段來填充recyclerview。 –

+0

思考我應該在webservice中做什麼修改。如果您有任何想法或數據庫設計策略,請與我分享。 –

0

所以你有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

數據源的結構是什麼。 –

相關問題