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?

Alexander Holsgrove
  • 1,795
  • 3
  • 25
  • 54

2 Answers2

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);
}
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • 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? – Alexander Holsgrove Sep 27 '13 at 13:32
  • So what's the point of item name ? it's anyway taking string name from @string – Bhupesh Aug 13 '21 at 04:58
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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40