Search code examples
androidandroid-listviewandroid-3.0-honeycombandroid-view

Android: First element list view acting weird


In my Android application I have a ListView with 5 elements. I created a custom adapter, in order to change the background of some elements of the listView. For example, the second item of the list view is not ready yet, so I want to setBackground(Color.Gray), so he can look like it's not done it. In order to do that, I Overrided the getView() method from ArrayAdapter in my custom adapter how it follows:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent); 
    if(!itensAvailable[position]) v.setBackgroundColor(Color.Gray);
    return v;
}

The weird thing is, no matter if I use the boolean itensAvailable[position] or !itensAvailable[position] the first element of the list always has it's background changed! All the others elements of the list are behaving like expected, except the first one. More weird than that, if I do if(position == 2) v.setBackgroundColor(Color.Gray);

it changes the background from the item in the position 2, and the first item as well! If I do

if(position == 2) {
v.setBackgroundColor(Color.Gray);
System.out.println(v.getText());
}

Even more weird! Only the text from the position 2 gets printed, not the text from the first item.

What is going on? Android bug? By the way, im testing it on a XOOM 3.2 Honeycomb device.

And obviously, if I comment this if code, the first item doens't has it's background changed.


Solution

  • It is very strange ! What happens if you write :

        @Override 
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent); 
            if(!itensAvailable[position]) v.setBackgroundColor(Color.Gray);
            else v.setBackgroundColor(Color.Transparent);
            return v;
        }