How to Set Background Color or Drawable to a Specific Item and when the listview is scrolled to have those colors stay
<p>I have a ListView that is populated with 50 items.
This is the xml that I use for my ArrayAdapter.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="@drawable/listview_background"
android:textColor="#ff000000"&gt;
&lt;/TextView&gt;
</code></pre>

<p>I type </p>

<pre><code>ArrayAdapter&lt;String&gt; list = new ArrayAdapter&lt;String&gt;(this, R.layout.my_simple_list); 
</code></pre>

<p>to create a new ArrayAdapter. Then I add a String[] items to the list. Then I call</p>

<pre><code>setListAdapter(list);
</code></pre>

<p>To add Strings from the String[] items to list, I use a for loop to loop through the String array and add each String to list using command: list.add(items.get(i)); where i is my for loop counter. </p>

<pre><code>for(int i=0;i&lt;items.size();i++){
list.add(items.get(i));
}
</code></pre>

<p>This gets my ListView created and populated. I also have an onListItemClick function.</p>

<pre><code>public void onListItemClick(ListView parent, View v, int position, long id) {
String Select = this.getListAdapter().getItem(position).toString();
if(copyitems.contains(Select)){
        v.setBackgroundResource(R.drawable.listview_background);
    copyitems.remove(Select);
}else{
    copyitems.add(Select);
    v.setBackgroundColor(Color.GRAY);
}       
}
</code></pre>

<p>In the onListItemClick function copyitems is an ArrayAdapter variable that I declare as a global variable. So what this function does is store the text from the selected items of the listview so later I can copy that selected text. So if an item is selected it's text is added to copyitems and if the same item is selected it will be removed form copyitems. Every time an item is added to copyitems the background of that item is changed to Gray and if an item is removed from copyitems the background of that item is set back to original. All this work fine. But when I have lots of items(like 50) they don't all fit to the screen so if I want to select an item that is not on screen then I have to scroll down or up to get to the item. The wierd this is that when I scroll away from a selected item and comeback to it the Background changes to original and a different item has the Gray background. So basically if my screen fits 10 items and I select the 1st one and scroll to select others that are passed 10(for example 11). When the 1st item leaves the screen and the 11 comes in, it is already colored Gray but I didn't selected. When I scroll back up to see the 1st item, the background may or may not be Gray. If its not Gray so other item's Background is set to Gray like the 2nd one. Is this happening because the items are recycled when they go off the screen and because of it the positions change of the items? If it is how can I disable that so I only have items background being gray if I have selected that item. If there is a better approach to what I'm trying to do please tell me. I'm new to android.    </p>
