【问题标题】:Adapter display png from resources doesn't work资源中的适配器显示 png 不起作用
【发布时间】:2015-01-02 18:42:53
【问题描述】:

我正在尝试通过其适配器在自定义微调器中显示 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()));

此行不为空,但不显示任何内容。我错过了什么吗?

【问题讨论】:

标签: android adapter android-spinner android-resources


【解决方案1】:

试试这个:

int identifier = context.getResources().getIdentifier(flagName, "drawable", context.getPackageName());
holder.imageFlag.setImageResource(identifier);

【讨论】:

    猜你喜欢
    • 2021-05-14
    • 2019-03-15
    • 2015-02-09
    • 2013-06-20
    • 2018-03-28
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2014-12-23
    相关资源
    最近更新 更多