I think this is a pretty simple question but I'm having trouble finding the answer out there...
I'm developing an android application (currently developing against v1.5 API) and one of the activities use a ListView. I want to be able to set the properties of each List Item based on the state of an in memory object rather than the state of the view or list item.
Here is a simple example....say I have a Person class who's public members are defined as follows:
public class Person {
public string getName() {...}
public boolean isYoung() {...}
public boolean isMiddleAged() {...}
public boolean isOld() {...}
}
Just like the example outlined above the properties in my class are mutually exclusive (so only one of the three boolean values may be true). Now say each List Item in my ListView is created from a Person object. I'm currently using a custom ArrayAdapter< Person> class to bind the Person objects to the List View (not sure if that matters or not but thought I'd mention it).
I want to be able to set various List Item properties (text color and style and background color) based on the values of each Person object that is bound in the List View. Like possibly making the background color of all young people green, middle aged people orange, and old people black. How can I achieve these results?
UPDATE: Thanks for your speedy reply Cristian C. I have been trying to implement your solution and keep getting this exception:
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6:
<item
> tag requires a 'drawable' attribute or child tag defining a drawable
...every time the setBackgroundResource() executes:
public View getView(int index, View convertView, ViewGroup parent) {
//...stuff here...
if(person.isYoung())
view.setBackgroundResource(R.drawable.green);
}
Where the selector 'green.xml' is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:background="@color/green" />
</selector>
Do you see any glaring mistakes with what I'm trying to do?
Thanks for your responses!