title: Checking values in SparseBooleanArray
body: <p>I'm trying to stop the user from adding more than four checked items. I obtain the selections using getCheckedItemPositions for my listview. Once a selection is checked, I place the selections in another SparseBooleanArray named OldCheckedItems. The goal is to compare the new list to the old list to determine which item was added last. It seems that the problem is the OldCheckedItems SparseBooleanArray is getting updated at the same time as CheckedItems, instead of one selection behind. The last value selected is always in the old list. What do I not understand?</p>
  
  <pre><code>package com.example.verytemptwo;
  
  import androidx.appcompat.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.SparseBooleanArray;
  import android.view.View;
  import android.view.inspector.StaticInspectionCompanionProvider;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.CursorAdapter;
  import android.widget.Button;
  import android.widget.Toast;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.ArrayAdapter;
  import java.lang.reflect.Array;
  import java.util.ArrayList;
  
  public class AddScoreMain extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_add_score_main);
  
          final ListView myListview = findViewById(R.id.mylistview);
  
          String[] OSs = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                  "Android", "iPhone", "WindowsMobile" };
  
          ArrayAdapter&lt;String&gt; myAdapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_multiple_choice, OSs);
          myListview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
          myListview.setAdapter(myAdapter);
  
          myListview.setOnItemClickListener(new OnItemClickListener() {
              SparseBooleanArray OldCheckedItems = new SparseBooleanArray();
              @Override
              public void onItemClick(AdapterView&lt;?&gt; myadapter, View view,
                                          int position, long id) {
  
                  SparseBooleanArray CheckedItems = myListview.getCheckedItemPositions();
                      for (int y = 0; y &lt; OldCheckedItems.size(); y++) {
                          Toast.makeText(getApplicationContext(), "Top of Loop: " + OldCheckedItems.keyAt(y), Toast.LENGTH_SHORT).show();
                      }
  
                  if (CheckedItems.size() &gt; 4) {
                      Toast.makeText(getApplicationContext(), "Too many" + String.valueOf(CheckedItems.size()), Toast.LENGTH_SHORT).show();
                      boolean found= false;
                      for (int x = 0; x &lt; CheckedItems.size(); x++) {
                          found = false;
                          for (int y = 0; y &lt; OldCheckedItems.size(); y++) {
  
                              if (OldCheckedItems.keyAt(y) == CheckedItems.keyAt(x)) {
                              found = true;
                              break;
                              }
                          }
                              if (!found) {
                                  Toast.makeText(getApplicationContext(), "---&gt;" + String.valueOf(CheckedItems.keyAt(x)), Toast.LENGTH_SHORT).show();
                                  //      myListview.removeViewAt(x);
                                  break;
                              }
                      }
                  }
                  else {
                      OldCheckedItems = myListview.getCheckedItemPositions();
  /*
                      if (CheckedItems.size() == 4) {
                          for (int y = 0; y &lt; OldCheckedItems.size(); y++) {
                          Toast.makeText(getApplicationContext(), "Last Four: " + String.valueOf(OldCheckedItems.keyAt(y)), Toast.LENGTH_SHORT).show();
                          }
                      }
  */
                  }
              }
          });
          Button nextbutton = findViewById(R.id.NextButton);
          nextbutton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
    //              startActivity(new Intent(MainActivity.this, AddScoreMain.class));
              }
          });
      }
  }
  </code></pre>