title: Insert value into database only when the Switchbutton changes
body: <p>I'm using a switchbutton to send informations to my sqlite database.</p>
  
  <p>When the Switchbutton is ON, inserts a value into a table, when the Switchbutton is OFF, deletes the same value.</p>
  
  <p>The problem is that, when I check the switchbutton, the value is sent, but when I close the switchbutton activity, and I come back, the record is inserted again, because the switchbutton appears ON.</p>
  
  <p>I would like to prevent this from checking if the value that I am going to send, already exists in the table.</p>
  
  <p>Here my code:</p>
  
  <pre><code>public void checkFavorite(){
  
      sbFavorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
              Favorites test = new Favorites(globalVar.getSessionUserId(), Integer.valueOf(splitExtras(LOCATION_ID)));
  
              if(isChecked) {
  
                  if (!connect.getAllFavorites().contains(test)){
                      connect.addFavorites(test);
  
              }
  
              }else{
  
  
                  if (connect.getAllFavorites().contains(test)){
                      connect.deleteFavorites(test);
  
                  }
  
  
              }
  
          }
      });
  
  
      sbFavorite.setChecked(Boolean.valueOf(splitExtras(LOCATION_FAVORITE)));
  
  }
  </code></pre>
  
  <p>The line: Boolean.valueOf(splitExtras(LOCATION_FAVORITE))</p>
  
  <p>Receives a boolean value from my database, it checks if the value that was sent is inserted into the table, returns false when this value doesn't exist.</p>