title: Change item background color in simple listView
body: <p>I would like to change an item background color when clicked, in a simple listView. Here's my code:</p>
  
  <pre><code>boolean[] selectedItem = new boolean[listElement.length]
  final ArrayList&lt;String&gt; list1 = new ArrayList&lt;&gt;();
  Collections.addAll(list1, listElement);
  final ListView mylist = (ListView) findViewById(R.id.listView);
  final ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;&gt;(context, android.R.layout.simple_list_item_1, list1);
  mylist.setAdapter(adapter);
  mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int pos, long arg3) {
  
              int firstVisiblePosition = mylist.getFirstVisiblePosition();
              int effectivePosition = pos - firstVisiblePosition;
  
              if (!selectedItem[pos]) {
                  mylist.getChildAt(effectivePosition).setBackgroundColor(Color.parseColor("#66F44336"));
              } else {
                  mylist.getChildAt(effectivePosition).setBackgroundColor(Color.parseColor("#EEEEEE"));
              }
  
             selectedItem[pos] = !selectedItem[pos];
          }
      });
  </code></pre>
  
  <p>When the list is short (no scroll involved) it works, when it's long it does not: the background color of the clicked item does change, but when I start to scroll the background color of <em>every</em> item starts to change, and I can't find any logic in those changes, they change and reverse without me even touching them, just by scrolling, wich is strange since the color should only change when onItemClick() is called, right? What am I missing?</p>