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?