title: display recyclerview checkbox as selected and un-selected based on boolean values android
body: <p>Let me tell you what exactly I needed. I need to load 20 questions from my MySql DB to recyclerview. Questions are saved in a table that has two columns ie, id and question. I can successfully load these questions to my recyclerview with the help of an adapter. Here is the twist, I need to mark these questions as "Yes" or "No" in my recyclerview by using checkboxes for each based on what exactly logged in user answered before this questions from his profile. Also, I expect good database and object class design solution in order to achieve this.</p>
  
  <p>Here what I have tried,</p>
  
  <p>Adapter Class:</p>
  
  <pre><code>public class MyHealthInfoAdapter extends RecyclerView.Adapter&lt;MyHealthInfoAdapter.MyHealthViewHolder&gt; {
  
  private Context context;
  private ArrayList&lt;DonorHealthStatusQuestionare&gt; listQuestions;
  
  public MyHealthInfoAdapter(Context context, ArrayList&lt;DonorHealthStatusQuestionare&gt; listQuestions) {
      this.context = context;
      this.listQuestions = listQuestions;
  }
  
  @Override
  public MyHealthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_my_health_info, parent, false);
      return new MyHealthViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(final MyHealthViewHolder holder, int position) {
  
      DonorHealthStatusQuestionare donorHealthStatusQuestionare = listQuestions.get(position);
      holder.tvQuestion.setText(donorHealthStatusQuestionare.getQuestions());
  
  }
  
  @Override
  public int getItemCount() {
      return listQuestions.size();
  }
  
  public class MyHealthViewHolder extends RecyclerView.ViewHolder{
  
      CardView cvParent;
      TextView tvQuestion;
      CheckBox cbYes, cbNo;
  
      public MyHealthViewHolder(View itemView) {
          super(itemView);
          cvParent = itemView.findViewById(R.id.card_view_my_health_status_rv);
          tvQuestion = itemView.findViewById(R.id.tv_eligibility_question_my_info);
          cbYes = itemView.findViewById(R.id.checkBoxYes_my_health_info);
          cbNo = itemView.findViewById(R.id.checkBoxNo_my_health_info);
  
      }
  }    
  }
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>public class DonorHealthStatus extends Fragment {
  
  private static final int MY_SOCKET_TIMEOUT_MS = 100000;
  private RecyclerView recyclerView;
  private RecyclerView.LayoutManager layoutManager;
  private DonorHealthStatusAdapter donorHealthStatusAdapter;
  private ArrayList&lt;DonorHealthStatusQuestionare&gt; donorHealthStatusQuestionares = new ArrayList&lt;&gt;();
  private HealthStatusCheck statusCheck = new HealthStatusCheck();
  private String donorID;
  private Donor donor;
  private boolean checked;
  
  
  public DonorHealthStatus() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_donor_health_status, container, false);
  }
  
  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
      donor = ((DisplayDonorDetails)getActivity()).getDonor();
      donorID = donor.getUserID();
  
      getHealthStatus(new HealthStatusCallBack() {
          @Override
          public void onSuccess(HealthStatusCheck healthStatusCheck) {
              statusCheck = healthStatusCheck;
              setHealthStatusView();
          }
  
          @Override
          public void onFail(String msg) {}
      });
  
  }
  
  // SETTING HEALTH STATUS POJO CLASS FROM DATABASE.
  private void getHealthStatus(final HealthStatusCallBack healthStatusCallBack) {
      Utility.checkNetworkConnectionBoolean(getActivity());
  
      if(Utility.isNetworkEnabled){
          StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GET_HEALTH_STATUS,
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
  
                          try {
  
                              JSONArray jsonArray = new JSONArray(response);
                              JSONObject jsonObject = jsonArray.getJSONObject(0);
                              String code = jsonObject.getString("code");
  
                              if(code.equals("0001")){
  
                                  statusCheck.setId(Integer.valueOf(jsonObject.getString("id")));
                                  statusCheck.setDonorID(Integer.valueOf(jsonObject.getString("donorID")));
  
                                  if(jsonObject.getString("q1").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q1").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q2").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q2").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q3").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q3").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q4").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q4").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q5").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q5").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q6").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q6").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q7").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q7").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q8").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q8").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q9").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q9").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q10").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q10").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q11").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q11").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q12").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q12").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q13").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q13").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q14").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q14").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q15").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q15").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q16").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q16").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q17").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q17").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q18").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q18").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q19").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q19").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q20").equals("0")){
                                      checked = false;
                                      statusCheck.setQ1(checked);
                                  }else if(jsonObject.getString("q20").equals("1")){
                                      checked = true;
                                      statusCheck.setQ1(checked);
                                  }
  
                              }
  
                              healthStatusCallBack.onSuccess(statusCheck);
  
  
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
  
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {}
                  }
          ){
              @Override
              protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
                  Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
                  params.put("donorID", donorID);
                  return params;
              }
          };
  
          stringRequest.setRetryPolicy(new DefaultRetryPolicy(MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                  DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
          NetworkRequestSingleTon.getOurInstance(getActivity()).addToRequestQue(stringRequest);
      }else {
          Utility.checkNetworkConnectionAlert(getActivity());
      }
  
  
  
  }
  
  // SETTING RECYCLER VIEW OF QUESTIONARE FROM DATABASE.
  private void setHealthStatusView() {
  
      recyclerView = getActivity().findViewById(R.id.rv_donor_health_info);
      layoutManager = new LinearLayoutManager(getActivity());
  
      recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
  
      donorHealthStatusAdapter = new DonorHealthStatusAdapter(getActivity(),
              donorHealthStatusQuestionares, statusCheck);
  
      recyclerView.setAdapter(donorHealthStatusAdapter);
  
      Utility.checkGpsConnectionBoolean(getActivity());
      if(Utility.isNetworkEnabled){
  
          StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GET_HEALTH_QUESTIONARE,
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
  
                          try {
  
                              JSONArray jsonArray = new JSONArray(response);
  
                              int count = 0;
  
                              while (count &lt; jsonArray.length()){
                                  JSONObject jsonObject = jsonArray.getJSONObject(count);
  
                                  DonorHealthStatusQuestionare donorHealthStatusQuestionare =
                                          new DonorHealthStatusQuestionare();
  
                                  donorHealthStatusQuestionare.setId(Integer.valueOf(jsonObject.getString("id")));
                                  donorHealthStatusQuestionare.setQuestions(jsonObject.getString("questions"));
  
                                  donorHealthStatusQuestionares.add(donorHealthStatusQuestionare);
                                  count ++;
  
                              }
  
                              donorHealthStatusAdapter.notifyDataSetChanged();
  
  
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
  
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
  
                      }
                  }
          );
          stringRequest.setRetryPolicy(new DefaultRetryPolicy(MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                  DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
          NetworkRequestSingleTon.getOurInstance(getActivity()).addToRequestQue(stringRequest);
  
      }else {
          Utility.checkNetworkConnectionAlert(getActivity());
      }
  
  }
  
  // CALL BACK INTERFACE TO GET POJO CLASS FROM VOLLEY BACKGROUND ACTIVITY.
  public interface HealthStatusCallBack{
  
      void onSuccess(HealthStatusCheck healthStatusCheck);
  
      void onFail(String msg);
  }
  }
  </code></pre>
  
  <p>LAYOUT XML:</p>
  
  <pre><code>&lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/card_view_health_status_rv"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="1dp"&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:baselineAligned="false"
      android:orientation="horizontal"
      android:padding="12dp"&gt;
  
      &lt;RelativeLayout
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginEnd="16dp"
          android:layout_marginRight="16dp"
          android:layout_weight="1"&gt;
  
          &lt;TextView
              android:id="@+id/tv_eligibility_question"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  
      &lt;RelativeLayout
          android:id="@+id/health_status_radio_group"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="0"&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBoxYes"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="16dp"
              android:layout_marginRight="16dp"
              android:text="@string/yes" /&gt;
  
          &lt;CheckBox
              android:layout_toRightOf="@+id/checkBoxYes"
              android:layout_toEndOf="@+id/checkBoxYes"
              android:id="@+id/checkBoxNo"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/no" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>Result Check POJO Class:</p>
  
  <pre><code>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;
  }
  }
  </code></pre>
  
  <p>Questions POJO class:</p>
  
  <pre><code>public class DonorHealthStatusQuestionare {
  private int id;
  private String questions;
  boolean isSelected;
  
  public DonorHealthStatusQuestionare() {}
  
  public DonorHealthStatusQuestionare(int id, String questions) {
      this.id = id;
      this.questions = questions;
  }
  
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  public String getQuestions() {
      return questions;
  }
  
  public void setQuestions(String questions) {
      this.questions = questions;
  }
  
  public boolean isSelected() {
      return isSelected;
  }
  
  public void setSelected(boolean selected) {
      isSelected = selected;
  }
  }
  </code></pre>