views:

53

answers:

1

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"&gt;
    <item android:background="@color/green" />
</selector>

Do you see any glaring mistakes with what I'm trying to do?

Thanks for your responses!

A: 

You will have to do it programatically. Let's suppose you have define all those selectors in a XML file; then you will have something like this:

private class YourAdapter extends ArrayAdapter<Person>{
    // foo bar baz
    //...

    public View getView(int position, View convertView, ViewGroup parent){

        // more foo bar baz

        if( person.isYoung() )
            view.setBackgroundResource(R.drawable.green);
        else if( person.isMiddledAge() )
            view.setBackgroundResource(R.drawable.orange);
        else if( person.isOld() )
            view.setBackgroundResource(R.drawable.black);// are you racist?  Just kidding

        return view;
    }
}

You can define your selector this way:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item 
        android:drawable="@drawable/green" />
</selector>

Green can be an image in the drawable folder called green.png. You can also do it using solid colors that you define in your colors.xml file (values directory):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="orange">#FF5721</color>
</resources>

Then, you would do:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item 
        android:drawable="@color/orange" />
</selector>
Cristian
Thanks for the reply Cristian...please see my update. Thanks again!
MoMo
I haven't seen your comment... I will update my answer...
Cristian
Perfect!! Thanks again for your help =)
MoMo