In my Android application, I have a ListView with 5 items. I created a custom adapter to change the background of some listView elements. For example, the second element of the list is not ready yet, so I want setBackground(Color.Gray)
, so it may look like it wasn’t. To do this, I redefined the getView () method from the ArrayAdapter in my user adapter, as 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; }
Strange, whether I use a logical itensAvailable[position]
or !itensAvailable[position]
, the first list item always changes the background! All other elements of the list behave as expected, except for the first. More strange if I do this if(position == 2) v.setBackgroundColor(Color.Gray);
it changes the background from position in position 2 to the first element! If i do
if(position == 2) { v.setBackgroundColor(Color.Gray); System.out.println(v.getText()); }
More weird! Only text from position 2 is printed, not text from the first element.
What's happening? Android bug? By the way, I tested it on a XOOM 3.2 Honeycomb device.
And, obviously, if I comment on this code, the first element will not change it.