어댑터를 통해 맞춤 회 전자 내부에 PNG를 표시하려고합니다.리소스의 어댑터 표시 png이 작동하지 않습니다.
이 스피너는 표시 순간에 다른 나라를 포함
COUNTRYNAME -
codePhone
그러나 나는이 얻을 수있는 플래그를 추가하고 싶습니다 :플래그. png - countryName - codePhone
그래서 나는 내 모든 PNG 플래그를 포함 당김 폴더에서 만든 : 당김/플래그/... "
여기내 어댑터 :
공용 클래스 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()));() countryNameDigits.entrySet (TreeMap.Entry ndItem위한
: }
} } return convertView; } static class ViewHolder { TextView countryName; TextView countryCode; ImageView imageFlag; } }
작동하지 않는 부분이 (a flag.png 표시한다)이다) { 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 ("드로어 블/사진 플래그 /"+ flagName + ". png ", null, context.getPackageName()));
이 줄은 null이 아니며 표시되지 않습니다. 내가 뭔가를 놓친 건가?
동작하는 예제 : http://fatoumatabintou.blogspot.it/2012/02/custom-spinner-with-icon-and-text.html –
만들 수있는 방법은 없습니다 자신의 drawable 하위 폴더. http://stackoverflow.com/questions/1077357/can-the-android-drawable-directory-contain-subdirectories – smora