Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 966019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:05:30+00:00 2026-05-16T02:05:30+00:00

I think this is a pretty simple question but I’m having trouble finding the

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:05:31+00:00Added an answer on May 16, 2026 at 2:05 am

    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">
        <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">
        <item 
            android:drawable="@color/orange" />
    </selector>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a pretty basic question, and I think I know the
I think the question is pretty self explanatory. Anyone done this before? UPDATE :
I think this must be simple but I can't get it right... I have
I think this must be a stupid question, but why do the results of
I think this is pretty typical, you have the same website project with an
I think this is specific to IE 6.0 but... In JavaScript I add a
I think this is a multi-part question, so bear with me. Currently all of
I think this will be easy but I can't see how to do it!
I think this should be easy, but it's evading me. I've got a many-to-many
I think this mostly applies to web applications, since you often see things like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.