title: How do I keep a switch on one RecyclerView item from influencing a switch on another item?
body: <p>I am making a simple alarm app that holds alarm data in a databased generated with Room, and uses a viewmodel to update the <code>RecyclerView</code> adapter's list. However, when I check/uncheck the switch on one item in the <code>RecyclerView</code> it sometimes causes another item's switch to check/uncheck itself, and I'm not sure why. Any help on what is wrong with my code is appreciated. Here is the java code:</p>
  <p>MainActivity:</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  private List&lt;Alarms&gt; mainAlarmsList;
  private RecyclerView recyclerView;
  private RecyclerView.LayoutManager layoutManager;
  private AlarmsAdapter alarmsAdapter;
  private AlarmsAdapter.alarmedInterface coolInterface;
  private ImageButton imageButton, clearButton;
  private AlarmsViewModel alarmsViewModel;
  private AlarmManager epicAlarmManager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      epicAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      imageButton = (ImageButton) findViewById(R.id.alarm_button);
      clearButton = (ImageButton) findViewById(R.id.clear_alarms_button);
  
      imageButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              createTimePicker(MainActivity.this);
          }
  });
      alarmsViewModel = ViewModelProviders.of(this).get(AlarmsViewModel.class);
  
      clearButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              alarmsViewModel.deleteAllAlarms();
          }
      });
      recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
      alarmsAdapter = new AlarmsAdapter(this);
      //alarmsAdapter.setAlarms(alarmsViewModel.getAllAlarms().getValue());
      coolInterface = new AlarmsAdapter.alarmedInterface() {
          @Override
          public void onSwitch(int position, boolean isChecked, CompoundButton compoundButton) {
  
              Alarms currentAlarm = mainAlarmsList.get(position);
              String concatenatedString;
              if(isChecked){
                  currentAlarm.setOffOn(1);
                  concatenatedString = currentAlarm.getId() + &quot; is on &quot; + currentAlarm.getOffOn();
                  compoundButton.setChecked(true);
              }
              else {
                  currentAlarm.setOffOn(0);
                  concatenatedString = currentAlarm.getId() + &quot; is off &quot; + currentAlarm.getOffOn();
                  compoundButton.setChecked(false);
  
              }
              Toast.makeText(MainActivity.this,concatenatedString,Toast.LENGTH_LONG).show();
              new updateAsync(alarmsViewModel).execute(currentAlarm);
          }
      };
  
      recyclerView.setAdapter(alarmsAdapter);
      alarmsViewModel.getAllAlarms().observe(this, new Observer&lt;List&lt;Alarms&gt;&gt;() {
          @Override
          public void onChanged(List&lt;Alarms&gt; alarms) {
              alarmsAdapter.setAlarms(alarms);
              alarmsAdapter.setAlarmInterfaceInstance(coolInterface);
              alarmsAdapter.notifyDataSetChanged();
              mainAlarmsList = alarms;
  
          }
      });
      layoutManager = new LinearLayoutManager(this);
      recyclerView.setLayoutManager(layoutManager);
  }
  public void createTimePicker(Context context){
      final Calendar calendar = Calendar.getInstance();
      final int hrDay = Calendar.HOUR_OF_DAY;
      final int min = Calendar.MINUTE;
      TimePickerDialog dialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() 
   {
  
          @Override
          public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
              Alarms myAlarm = new Alarms(hourOfDay,minute);
              myAlarm.setOffOn(1);
              alarmsViewModel.insertAlarms(myAlarm);
              alarmsAdapter.notifyDataSetChanged();
              calendar.set(Calendar.HOUR_OF_DAY, myAlarm.getHour());
              calendar.set(Calendar.MINUTE, myAlarm.getMin());
              alarmTime(myAlarm,calendar);
          }
      }, hrDay,min,true);
      dialog.show();
  }
  public void alarmTime(Alarms alarms,Calendar calendar){
      Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,
              alarms.getId(),intent,0);
      long hrlong = alarms.getHour();
      long minlong = alarms.getMin();
      epicAlarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() ,pendingIntent);
    }
    class updateAsync extends AsyncTask&lt;Alarms,Void,Void&gt;{
      private AlarmsViewModel viewModel;
      updateAsync(AlarmsViewModel alarmsViewModel1){
          viewModel = alarmsViewModel1;
      }
      @Override
      protected Void doInBackground(Alarms... alarms) {
          viewModel.updateAlarms(alarms[0]);
          return null;
      }
  }
  
  }
  </code></pre>
  <p>Alarms.java</p>
  <pre><code>import androidx.room.ColumnInfo;
  import androidx.room.Entity;
  import androidx.room.PrimaryKey;
  
  @Entity(tableName = &quot;Alarms&quot;)
  public class Alarms {
  @PrimaryKey(autoGenerate = true)
  private int id;
  @ColumnInfo(name = &quot;hour&quot;)
  private int hour;
  @ColumnInfo(name = &quot;min&quot;)
  private int min;
  @ColumnInfo(name = &quot;offOn&quot;)
  private int offOn;
  
  public Alarms(int hour, int min){
      this.hour = hour;
      this.min = min;
      offOn = 1;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  public void setMin(int min) {
      this.min = min;
  }
  
  public void setHour(int hour) {
      this.hour = hour;
  }
  
  public void setOffOn(int offOn) {
      this.offOn = offOn;
  }
  
  public int getMin() {
      return min;
  }
  
  public int getHour() {
      return hour;
  }
  
  public int getOffOn() {
      return offOn;
  }
  
  public int getId(){
      return id;
  }
  }
  </code></pre>
  <p>AlarmsAdapter.java</p>
  <pre><code>import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.CompoundButton;
  import android.widget.Switch;
  import android.widget.TextView;
  import android.widget.Toast;
  import androidx.annotation.NonNull;
  import androidx.appcompat.widget.SwitchCompat;
  import androidx.recyclerview.widget.RecyclerView;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class AlarmsAdapter extends RecyclerView.Adapter&lt;AlarmsAdapter.AlarmsViewHolder&gt; {
  private Context context;
  private List&lt;Alarms&gt; alarms;
  private alarmedInterface alarmInterfaceInstance;
  interface alarmedInterface{
      public void onSwitch(int position, boolean checked, CompoundButton compoundButton);
  }
  public AlarmsAdapter(Context context){
      this.context = context;
  }
  
  public void setAlarmInterfaceInstance(alarmedInterface alarmInterfaceInstance) {
      this.alarmInterfaceInstance = alarmInterfaceInstance;
  }
  
  public void setAlarms(List&lt;Alarms&gt; alarms) {
      this.alarms = alarms;
  }
  
  @NonNull
  @Override
  public AlarmsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);
      return new AlarmsViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(@NonNull AlarmsAdapter.AlarmsViewHolder holder, final int position) {
      if(alarms.isEmpty()){
          holder.textView.setText(&quot;No alarms&quot;);
          return;
      }
      else{
          Alarms myAlarm = alarms.get(position);
      if(myAlarm.getOffOn() == 1){
          holder.aSwitch.setChecked(true);
      }
      else if(myAlarm.getOffOn() == 0){
          holder.aSwitch.setChecked(false);
      }
          String x = alarms.get(position).getHour() + &quot;:&quot; + alarms.get(position).getMin();
          holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  alarmInterfaceInstance.onSwitch(position, isChecked,buttonView);
              }
          });
          holder.textView.setText(x);
      }
  }
  
  @Override
  public int getItemCount() {
      if(alarms != null) {
          return alarms.size();
      }
      else {
          return 0;
      }
  }
  public class AlarmsViewHolder extends RecyclerView.ViewHolder{
      TextView textView;
      SwitchCompat aSwitch;
      public AlarmsViewHolder(View view){
          super(view);
          textView = view.findViewById(R.id.textView);
          aSwitch = view.findViewById(R.id.switch1);
      }
  }
  }
  </code></pre>
  <p>AlarmsViewModel.java</p>
  <pre><code>import android.app.Application;
  import android.content.Context;
  
  import androidx.lifecycle.AndroidViewModel;
  import androidx.lifecycle.LiveData;
  
  import java.util.List;
  
  public class AlarmsViewModel extends AndroidViewModel {
  private AlarmsRepository repository;
  private LiveData&lt;List&lt;Alarms&gt;&gt; allAlarms;
  public AlarmsViewModel(Application application){
      super(application);
      repository = new AlarmsRepository(application.getApplicationContext());
      allAlarms = repository.getAlarmsList();
  }
  public void deleteAllAlarms(){
      repository.deleteAllAlarms();
  }
  public void updateAlarms(Alarms alarms){
      repository.updateAlarm(alarms);
  }
  public void insertAlarms(Alarms alarms){
      repository.insertAlarm(alarms);
  }
  
  public LiveData&lt;List&lt;Alarms&gt;&gt; getAllAlarms() {
      return allAlarms;
  }
  }
  </code></pre>
  <p>AlarmdDao.java</p>
  <pre><code>import androidx.lifecycle.LiveData;
  import androidx.room.Dao;
  import androidx.room.Delete;
  import androidx.room.Insert;
  import androidx.room.Query;
  import androidx.room.Update;
  
  import java.util.List;
  
  @Dao
  public interface AlarmDao {
  @Query(&quot;Select * From Alarms&quot;)
  public LiveData&lt;List&lt;Alarms&gt;&gt; getAllAlarms();
  @Insert
  public void insertAlarm(Alarms alarms);
  @Update
  public void updateAlarm(Alarms alarms);
  @Delete
  public void deleteAlarm(Alarms alarms);
  @Query(&quot;Delete From Alarms&quot;)
  public void deleteAllAlarms();
  }
  
  AlarmsDatabase
  import android.app.Application;
  import android.content.Context;
  
  import androidx.room.Database;
  import androidx.room.Room;
  import androidx.room.RoomDatabase;
  @Database(entities = {Alarms.class},version = 1,exportSchema = false)
  public abstract class AlarmsDatabase extends RoomDatabase {
  private static AlarmsDatabase alarmsDatabase;
  private Context context;
  public abstract AlarmDao alarmDao();
  public static AlarmsDatabase AlarmsDatabaseBuilder(Context context){
          if(alarmsDatabase == null){
              synchronized (AlarmsDatabase.class){
                  if (alarmsDatabase == null){
                      alarmsDatabase = Room.databaseBuilder(context.getApplicationContext(),
                              AlarmsDatabase.class,&quot;AlarmsDatabase&quot;).build();
                  }
          }
      }
          return alarmsDatabase;
  }
  }
  </code></pre>
  <p>AlarmReceiver.java</p>
  <pre><code>package com.pbaileyapps.android.chatpal;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.widget.Toast;
  
  public class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      Toast.makeText(context,&quot;You just recieved a toast!&quot;,Toast.LENGTH_LONG).show();
  }
  }
  </code></pre>