Monday, October 27, 2014

Android : Wrong backgroundcolor on ListView inflate



So I have a ListView that uses a custom adapter. I want the background color of an item be either red or green, depending on a value I get out of my database with getDBValue(); (this works).


The problem is, when the activity is first opened, and so the listview is inflated, all listview items have the same (not correct) green background color. When I start scrolling though, the background are set correctly.


How do I get the right background color when listview inflates and where does my program "error"?


Many thanks.


This is my code


getView():



public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mResource);
}

private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View v;

if (convertView == null) {
// first time views are created
v = mInflater.inflate(resource, parent, false);
} else {
v = convertView;
}

String dbvalue = getDBValue();

if(!dbvalue.equals("me")){
v.setBackgroundColor(Color.argb(230, 255, 0x00, 0x00));
}else{
v.setBackgroundColor(Color.argb(230, 0x00, 255, 0x00));
}

setViewText((TextView) v, text);

return v;
}

No comments:

Post a Comment