title: Auto check Checkbox on comparing the values from json
body: <p><a href="https://i.stack.imgur.com/TfP5I.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TfP5I.jpg" alt="json data from server"></a></p>
  
  <p>I want to check the roll and roll check days if they are equal then auto check that check box and can also unchecked that if needed by user.</p>
  
  <p>1.)Check if roll and <code>rollcheck</code> data from Json are equal then check the <code>checkbox</code> automatically.</p>
  
  <p>2.)user can also uncheck that automatically checked checkbox</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      Toolbar toolbar;
      FloatingActionButton fab;
      ListView list;
      TextView txt_menu;
      String dipilih;
      private static final String TAG = MainActivity.class.getSimpleName();
  
      Adapter adapter;
      ProgressDialog pDialog;
  
      List&lt;Data&gt; itemList = new ArrayList&lt;Data&gt;();
  
  
      private static String url = "http://url.php";
  
      public static final String TAG_NAMA = "nama";
       public static final String TAG_ROLL = "roll";
      public static final String TAG_ROLLCheck = "rollcheck";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          fab     = (FloatingActionButton) findViewById(R.id.fab);
          list    = (ListView) findViewById(R.id.list_menu);
  
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  String checkbox = "";
                  for (Data hold : adapter.getAllData()) {
                      if (hold.isCheckbox()) {
                          checkbox += "\n" + hold.getMenu();
                      }
                  }
                  if (!checkbox.isEmpty()) {
                      dipilih = checkbox;
                  } else {
                      dipilih = "No check box selected";
                  }
  
                  formSubmit(dipilih);
              }
          });
  
          callVolley();
  
          adapter = new Adapter(this, (ArrayList&lt;Data&gt;) itemList);
          list.setAdapter(adapter);
  
          list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, 
    int position, long l) {
                  adapter.setCheckBox(position);
              }
          });
  
      }
  
      private void formSubmit(String hasil){
          AlertDialog.Builder dialog = new AlertDialog.Builder(this);
          LayoutInflater inflater = getLayoutInflater();
          View dialogView = inflater.inflate(R.layout.form_submit, null);
          dialog.setView(dialogView);
          dialog.setIcon(R.mipmap.ic_launcher);
          dialog.setTitle("title");
          dialog.setCancelable(true);
  
          txt_menu = (TextView) dialogView.findViewById(R.id.txt_menu);
  
          txt_menu.setText(hasil);
  
          dialog.setNeutralButton("CLOSE", new 
       DialogInterface.OnClickListener() {
  
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.dismiss();
              }
          });
  
          dialog.show();
      }
  
      private void callVolley(){
          itemList.clear();
          // menapilkan dialog loading
          pDialog = new ProgressDialog(this);
          pDialog.setMessage("Loading...");
          showDialog();
  
          // membuat request JSON
          JsonArrayRequest jArr = new JsonArrayRequest(url, new 
         Response.Listener&lt;JSONArray&gt;() {
                      @Override
                      public void onResponse(JSONArray response) {
                          Log.d(TAG, response.toString());
                          hideDialog();
  
                          // Parsing json
                          for (int i = 0; i &lt; response.length(); i++) {
                              try {
                                  JSONObject obj = response.getJSONObject(i);
  
                                  Data item = new Data();
  
                                  item.setMenu(obj.getString(TAG_NAMA));
  
                                   item.setRoll(obj.getString(TAG_ROLL));
  
            item.setRollCheck(obj.getString(TAG_ROLLCheck));
  
                                  // menambah item ke array
                                  itemList.add(item);
                              } catch (JSONException e) {
                                  e.printStackTrace();
                              }
                          }
  
                          // notifikasi adanya perubahan data pada adapter
                          adapter.notifyDataSetChanged();
                      }
                  }, new Response.ErrorListener() {
  
              @Override
              public void onErrorResponse(VolleyError error) {
                  VolleyLog.d(TAG, "Error: " + error.getMessage());
                  hideDialog();
              }
          });
  
          // menambah request ke request queue
          AppController.getInstance().addToRequestQueue(jArr);
      }
  
      private void showDialog() {
          if (!pDialog.isShowing())
              pDialog.show();
      }
  
      private void hideDialog() {
          if (pDialog.isShowing())
              pDialog.dismiss();
      }
  
  }
  </code></pre>
  
  <p><strong>Adapter.java</strong></p>
  
  <pre><code>public class Adapter extends BaseAdapter {
  
      private Context activity;
      private ArrayList&lt;Data&gt; data;
      private static LayoutInflater inflater = null;
      private View vi;
      private ViewHolder viewHolder;
  
      public Adapter(Context context, ArrayList&lt;Data&gt; items) {
          this.activity = context;
          this.data = items;
          inflater = 
   (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public int getCount() {
          return data.size();
      }
  
      @Override
      public Object getItem(int i) {
          return i;
      }
  
      @Override
      public long getItemId(int i) {
          return i;
      }
  
      @Override
      public View getView(int position, View view, ViewGroup viewGroup) {
          vi = view;
          final int pos = position;
          Data items = data.get(pos);
  
          if(view == null) {
              vi = inflater.inflate(R.layout.list_row, null);
              viewHolder = new ViewHolder();
              viewHolder.checkBox = (CheckBox) vi.findViewById(R.id.cb);
              viewHolder.menu = (TextView) vi.findViewById(R.id.nama_menu);
              viewHolder.roll = (TextView) vi.findViewById(R.id.roll);
              vi.setTag(viewHolder);
          }else {
              viewHolder = (ViewHolder) view.getTag();
              viewHolder.menu.setText(items.getMenu());
              viewHolder.roll.setText(items.getRoll());
          }
  
          if(items.isCheckbox()){
              viewHolder.checkBox.setChecked(true);
          } else {
              viewHolder.checkBox.setChecked(false);
          }
  
          return vi;
      }
  
      public ArrayList&lt;Data&gt; getAllData(){
          return data;
      }
  
      public void setCheckBox(int position){
          Data items = data.get(position);
          items.setCheckbox(!items.isCheckbox());
          notifyDataSetChanged();
      }
  
      public class ViewHolder{
          TextView roll;
          TextView menu;
          CheckBox checkBox;
      }
   }
  </code></pre>
  
  <p><strong>Data.java</strong></p>
  
  <pre><code>public class Data {
      private String roll;
      private String menu;
      private boolean check;
      private String roll_check;
      public Data() {}
  
      public Data(String roll,String menu, boolean check,String roll_check) {
          this.roll=roll;
          this.menu = menu;
          this.check = check;
          this.roll_check = roll_check;
      }
  
      public String getRoll() {
          return roll;
      }
  
      public void setRoll(String roll) {
          this.roll = roll;
      }
  
      public String getRollCheck() {
          return roll_check;
      }
  
      public void setRollCheck(String roll_check) {
          this.roll_check = roll_check;
      }
  
   public String getMenu() {
          return menu;
      }
  
      public void setMenu(String menu) {
          this.menu = menu;
      }
  
      public boolean isCheckbox() {
          return check;
      }
  
      public void setCheckbox(boolean check) {
          this.check = check;
      }
  }
  </code></pre>