I'm trying to display a png inside a custom spinner through its adapter.
This spinner contains different countries at the moment it displays:
countryName - codePhone
But I would like to add a flag to get this:
flag.png - countryName - codePhone
So I have created in drawable a folder which contains all my png flags: drawable/flags/..."
Here my adapter:
public class SpinnerCountryAdapter extends BaseAdapter {
private Context context; private LayoutInflater inflater; private TreeMap<String, String> countryNameDigits = new TreeMap<String, String>(); private TreeMap<String, String> countryCodeName = new TreeMap<String, String>(); private String[] keys; private CountryCodePhone ccp; //////////////////////////////////////////////////////////////////////////////////////////////// public SpinnerCountryAdapter(Context context, CountryCodePhone ccp) { this.ccp = ccp; this.context = context; this.countryNameDigits = ccp.getCountryNameDigits(); this.countryCodeName = ccp.getCountryCodeName(); this.keys = countryNameDigits.keySet().toArray(new String[ccp.getCountryNameDigits().size()]); inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } //////////////////////////////////////////////////////////////////////////////////////////////// @Override public int getCount() { return countryNameDigits.size(); } @Override public String getItem(int position) { return countryNameDigits.get(keys[position]); } @Override public long getItemId(int position) { return 0; } public int getPosition(String s){ int position = 0; for (String key : countryNameDigits.keySet()) { if (key.equalsIgnoreCase(s)){ return position; }else{ position++; } } return 0; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } @Override public View getView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } //////////////////////////////////////////////////////////////////////////////////////////////// private View getCustomView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.custom_country_spinner, null); holder.countryName = (TextView) convertView.findViewById(R.id.countryname); holder.countryCode = (TextView) convertView.findViewById(R.id.countrycode); holder.imageFlag = (ImageView) convertView.findViewById(R.id.countryflag); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } String key_countryName = keys[position]; String value_countryCode = getItem(position).toString(); holder.countryName.setText(key_countryName); holder.countryCode.setText(value_countryCode); //TODO display correct flag - country<Name, Digits> / country<Code, Name> for (TreeMap.Entry<String, String> ndItem : countryNameDigits.entrySet()) { String key_name = ndItem.getKey(); for (TreeMap.Entry<String, String> cnItem : countryCodeName.entrySet()) { String key_code = cnItem.getKey(); String value_name = cnItem.getValue().toString(); if (key_name.equalsIgnoreCase(value_name)){ String flagName = key_code.toLowerCase(); holder.imageFlag.setImageResource(context.getResources().getIdentifier("drawable/flags/"
+ flagName+".png", null, context.getPackageName())); }
} } return convertView; } static class ViewHolder { TextView countryName; TextView countryCode; ImageView imageFlag; } }
The part which doesn't work is this one (which should display a flag.png):
for (TreeMap.Entry ndItem : countryNameDigits.entrySet()) { String key_name = ndItem.getKey();
for (TreeMap.Entry<String, String> cnItem : countryCodeName.entrySet()) { String key_code = cnItem.getKey(); String value_name = cnItem.getValue().toString(); if (key_name.equalsIgnoreCase(value_name)){ String flagName = key_code.toLowerCase(); holder.imageFlag.setImageResource(context.getResources().getIdentifier("drawable/flags/"
+ flagName+".png", null, context.getPackageName())); }
} }
And here I'm not sure about :
holder.imageFlag.setImageResource(context.getResources().getIdentifier("drawable/flags/" + flagName+".png", null, context.getPackageName()));
This line is not null but nothing is displayed. Did I missed something ?