title: android checkbox buttonview gets the initial and last id of checkbox when used in a for loop
body: <pre><code>    private void show_weatherdialog() {
  
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          LayoutInflater li = getLayoutInflater();
  </code></pre>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>inflating the main layout for the row_item that contains tetview, checkbox and add button</p>
  
  <pre><code>        View v = li.inflate(R.layout.custom_weather_layout, null);
          builder.setView(v);
  
          add = (Button) v.findViewById(R.id.addLocation);
          container = (LinearLayout) v.findViewById(R.id.container);
  
          //retrieving values from the database that has fields: id, location_id and location_title. currently there are 7 entries in the db.
          list = Dbhelper.getAllvalues();
          for (i = 0; i &lt; list.size()/*7*/ ; i++) {
              view = LayoutInflater.from(this).inflate(R.layout.custom_weather_item_layout,
                      null);
              location_title = (TextView) view.findViewById(R.id.location_title);
              location_id = (TextView) view.findViewById(R.id.location_id);
              check = (CheckBox) view.findViewById(R.id.checkbox);
              //setting id for each checkbox according to loop
              check.setId(i);
              //checking the id in Log
              id = check.getId();
              Log.d("checkbox id", "" + id);
  
              location_title.setText(list.get(i).location_title);
              location_id.setText(list.get(i).location_id);
              System.out.println("ADDVIEW:");
              container.addView(view);
  
              check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                  @Override
  
  
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  </code></pre>
  
  <blockquote>
    <p>the buttonView.getId() gets the first and last id</p>
  </blockquote>
  
  <p>int iid = buttonView.getId();
                          Log.d("Buttonview getid", "" + iid);</p>
  
  <pre><code>                    switch (iid) {//THE BUTTONVIEW ALWAYS GETS ID 0 AND 6 when the first checkbox is checked
                          case 0:
                              //Kathmandu For the first location
                              a = list.get(id).location_id;
                              checkedvalue = buttonView.isChecked();
                              Log.d("SELECTED", "" + a + " " + checkedvalue);
                              if (checkedvalue) {
                                  check.setChecked(true);
                                  array.add(a);
                                  Log.d("INSERTED INTO ARRAY", "true" + array);
                              } else {
                                  check.setChecked(false);
                                  array.remove(a);
                                  Log.d("UNSELECTED", "true");
                                  Log.d("DELETED FROM ARRAY", "true" + array);
                              }
                              break;
  
                          case 1:
                              //for second location
                              b = list.get(id).location_id;
                              checkedvalue = buttonView.isChecked();
  
                              Log.d("SELECTED", "" + b);
                              if (checkedvalue) {
                                  check.setChecked(true);
                                  array.add(b);
                                  Log.d("INSERTED INTO ARRAY", "true" + array);
                              } else {
                                  check.setChecked(false);
                                  array.remove(b);
                                  Log.d("UNSELECTED", "true" + array);
                                  Log.d("DELETED FROM ARRAY", "true" + array);
                              }
                              break;
  
                          case 2:
                              //Bhaktapur ko lagi
                              c = list.get(id).location_id;
                              checkedvalue = buttonView.isChecked();
                              Log.d("SELECTED", "" + c);
  
                              if (checkedvalue) {
                                  check.setChecked(true);
                                  array.add(c);
                                  Log.d("INSERTED INTO ARRAY", "true" + array);
                              } else {
                                  check.setChecked(false);
                                  array.remove(c);
                                  Log.d("UNSELECTED", "true");
                                  Log.d("DELETED FROM ARRAY", "true" + array);
                              }
                              break;
                          case 3:
                              //dhading ko lagi
                              d = list.get(id).location_id;
                              checkedvalue = buttonView.isChecked();
                              Log.d("SELECTED", "" + d);
  
                              if (checkedvalue) {
                                  check.setChecked(true);
                                  array.add(d);
                                  Log.d("INSERTED INTO ARRAY", "true" + array);
                              } else {
                                  check.setChecked(false);
                                  array.remove(d);
                                  Log.d("UNSELECTED", "true");
                                  Log.d("DELETED FROM ARRAY", "true" + array);
                              }
                              break;
                          case 4:
                              //nuwakot ko lagi
                              e = list.get(id).location_id;
                              checkedvalue = buttonView.isChecked();
                              Log.d("SELECTED", "" + e);
  
                              if (checkedvalue) {
                                  check.setChecked(true);
                                  array.add(e);
                                  Log.d("INSERTED INTO ARRAY", "true" + array);
                              } else {
                                  check.setChecked(false);
                                  array.remove(e);
                                  Log.d("UNSELECTED", "true");
                                  Log.d("DELETED FROM ARRAY", "true" + array);
                              }
                              break;
                          case 5:
                              //kavre ko lagi
                              f = list.get(id).location_id;
                              checkedvalue = buttonView.isChecked();
                              Log.d("SELECTED", "" + f);
  
                              if (checkedvalue) {
                                  check.setChecked(true);
                                  array.add(f);
                                  Log.d("INSERTED INTO ARRAY", "true" + array);
                              } else {
                                  check.setChecked(false);
                                  array.remove(f);
                                  Log.d("UNSELECTED", "true");
                                  Log.d("DELETED FROM ARRAY", "true" + array);
                              }
                              break;
                          case 6:
                              //makwanpur ko lagi
                              g = list.get(id).location_id;
                              checkedvalue = buttonView.isChecked();
                              Log.d("SELECTED", "" + g);
  
                              if (checkedvalue) {
                                  check.setChecked(true);
                                  array.add(g);
                                  Log.d("INSERTED INTO ARRAY", "true" + array);
                              } else {
                                  check.setChecked(false);
                                  array.remove(g);
                                  Log.d("UNSELECTED", "true");
                                  Log.d("DELETED FROM ARRAY", "true" + array);
                              }
                              break;
                          default:
                            break;
  
                      }
                      //adding the values of array in the loc_idlist arraylist to insert into another table in database
                      loc_idlist.addAll(array);
                      //Log.d("Arraylist ko values haru", "" + loc_idlist.size() + "" + loc_idlist);
                  }
              });
          }
  
          dialog = builder.create();
          dialog.show();
  
          add.setOnClickListener(new View.OnClickListener() {//add the arraylist into the database
              @Override
              public void onClick(View v) {
                  Log.d("Arraylist values: ", "" + loc_idlist.size() + "" + loc_idlist);
                  //display the values of arraylist
                  Toast.makeText(getApplication(), "Arraylist values:" + loc_idlist, Toast.LENGTH_SHORT).show();
                  dialog.dismiss();
  
              }
          });
      }
  </code></pre>