0

I am working through a tutorial on lists (and creating custom list views) and have a query regarding checking the value of a list item.

The getView method checks to see what image to display based on the value of the list item (using equals):

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

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.list_items, parent, false);
    String[] items = getResources().getStringArray(R.array.countries);

    ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
    TextView tv = (TextView) row.findViewById(R.id.textView1);

    tv.setText(items[position]);

    if(items[position].equals("United States")) {
        iv.setImageResource(R.drawable.usa);
    } else if(items[position].equals("Brazil")) {
        iv.setImageResource(R.drawable.brazil);
    } else if(items[position].equals("Russia")) {
        iv.setImageResource(R.drawable.russia);
    } else if(items[position].equals("Japan")) {
        iv.setImageResource(R.drawable.japan);
    }

    return row;
}

countries.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countries">
        <item name="usa">United States</item>
        <item name="brazil">Brazil</item>
        <item name="russia">Russia</item>
        <item name="japan">Japan</item>
    </string-array>
</resources>

I'd like to check against the item "name" property. Is this possible?

My question arises as I am thinking of creating a language specific copy of countries.xml and whilst the item name would be the same, the text would be different and I assume this would break my code?

2
  • I think it is possible, but it might not be worth it... Check this out. Commented Sep 27, 2013 at 11:50
  • Have you ever tried setTag() function? Commented Sep 27, 2013 at 11:54

2 Answers 2

2

Just write

    <item name="usa">@string/usa</item>
    <item name="brazil">@string/brazil</item>
    <item name="russia">@string/russia</item>
    <item name="japan">@string/japan</item>

you will have to create 4 strings as follows

    <string name="usa">United States</string>
    <string name="brazil">Brazil</string>
    <string name="russia">Russia</string>
    <string name="japan">Japan</string>

You can do the checking as follows.

if (items[position].equals(context.getResources().getString(R.string.usa))) {
    iv.setImageResource(R.drawable.usa);
} else if (items[position].equals(context.getResources().getString(R.string.brazil))) {
    iv.setImageResource(R.drawable.brazil);
} else if (items[position].equals(context.getResources().getString(R.string.russia))) {
    iv.setImageResource(R.drawable.russia);
} else if (items[position].equals(context.getResources().getString(R.string.japan))) {
    iv.setImageResource(R.drawable.japan);
}
Sign up to request clarification or add additional context in comments.

2 Comments

The first part is perfect - thank you. Is there a more efficient way to set the imageResource without having so many if/else statements (lets say if I had 100 items). Can I set the resource using the item name?
So what's the point of item name ? it's anyway taking string name from @string
0

Why not use LevelListDrawable?

Something like this:

LevelListDrawable image = (LLD) getResources().getDrawable(R.drawable.flags);
image.setLevel(position);
iv.setImageDrawable(image);

This will also allow you to add more flags & strings by touching only XML files and no Java code.

Edit: you probably want to use setTag()/getTag() to reuse the drawable, as loading it anew for each row may be slow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.