我正在尝试通过其适配器在自定义微调器内显示一个png。
此微调器在显示时包含不同的国家/地区:
countryName - codePhone
但是我想添加一个标志来实现这个目标:
flag.png - countryName - codePhone
所以我在drawable中创建了一个包含所有png标志的文件夹:drawable / flags /...“
这是我的适配器:
公共类SpinnerCountryAdapter扩展了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; } }
不起作用的部分是这一部分(应显示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())); }
} }
在这里,我不确定:
holder.imageFlag.setImageResource(context.getResources()。getIdentifier(“drawable / flags /”+ flagName +“。png”,null,context.getPackageName()));
此行不为空,但不显示任何内容。我错过了什么吗?
答案 0 :(得分:0)
试试这个:
int identifier = context.getResources().getIdentifier(flagName, "drawable", context.getPackageName());
holder.imageFlag.setImageResource(identifier);