Android: Custom ListView OnItemClickListener Weird Issues
<p>I have a ListView that I'm populating with dummy strings right now. It is currently holding 10 items, but the view itself is only large enough to show 5. So the list has to scroll of course. I've built the list using the following code:</p>

<p>ListView:</p>

<pre><code>&lt;RelativeLayout
    android:id="@+id/catList"
    android:layout_width="wrap_content"
    android:layout_height="216dip"
    android:layout_marginTop="120dip"
    &gt;
    &lt;ListView
        android:id="@+id/categoryList"
        android:layout_width="308dip"
        android:layout_height="216dip"
        android:layout_marginLeft="6dip"
        android:background="#ffffff"
        android:divider="#ffffff"
        android:choiceMode="singleChoice"
        android:cacheColorHint="#00000000"

        &gt;
    &lt;/ListView&gt;
    &lt;ImageView
        android:id="@+id/catListFrame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:layout_alignParentTop="true"
        android:src="@drawable/selectorframe"
        /&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>So basically there is an image frame around the list. This is all nested inside another layout.</p>

<p>I have a custom item xml file as well that looks like this:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="?android:attr/listPreferredItemHeight"
   android:padding="10dip"
   android:background="#ffffff"
   &gt;
   &lt;TextView
       android:id="@+id/catTitle"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:textSize="20dip"
       android:textColor="#000000"
   &gt;
   &lt;/TextView&gt;
 &lt;/LinearLayout&gt;
</code></pre>

<p>Here's the java that makes it all work:</p>

<pre><code>private void setCategoryControls(){
    btnBackHome = (ImageView)findViewById(R.id.catHomeBtn);
    btnBackHome.setOnClickListener(goHome);
    catList = (ListView)findViewById(R.id.categoryList);
    catList.setOnItemClickListener(selectCat);
    demoCats = new ArrayList&lt;String&gt;();
    for(int i=1;i&lt;=10;i++){
        demoCats.add("Item " + i);
    }
    m_catAdapter = new CatAdapter(this,R.layout.catitem,demoCats);
    catList.setAdapter(m_catAdapter);

}

private OnItemClickListener selectCat = new OnItemClickListener(){
    public void onItemClick(AdapterView&lt;?&gt; a, View v, int position, long id){
        if(selCatView!=null){
            selCatView.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        v.setBackgroundColor(Color.parseColor("#ffaa00"));
        selCatView = v;
    }
};
private class CatAdapter extends ArrayAdapter&lt;String&gt;{

    private ArrayList&lt;String&gt; items;

    public CatAdapter(Context context, int textViewResourceId, ArrayList&lt;String&gt; items){
        super(context, textViewResourceId, items);
        this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.catitem, null);
            }
            String cat = items.get(position);
            if (cat != null) {
                    TextView catName = (TextView) v.findViewById(R.id.catTitle);
                    if (catName != null) {
                          catName.setText(cat);
                    }

            }
            return v;
    }
}
</code></pre>

<p>What happens is when I click an item in the list it should simply change the background color of that item, instead it usually highlights 2 items (one that's visible and one that I have to scroll to), and most of the time it's not even the right item. Also if I scroll while an item is highlighted it will randomly change around which is highlighted as I scroll the highlighted item on and off the screen. If I had to guess, I'd say it's tied to the fact that the ListView apparantly only keeps track of the visible children. Even though it has an adapter with 10 items, if I run a getChildCount on the list view it only shows 5 or 6 depending on if one is partially visible or not, but it never shows more or less than the number of visible items.</p>
