使用drawable解析本地xml android

我想从
android中的assets文件夹解析一个本地
XML文件,我找到了一个很好的教程:
http://theopentutorials.com/tutorials/android/xml/android-simple-xmlpullparser-tutorial/

一切都很好,我已经转换了教程代码,以获得一个很好的列表视图,其中包含每个行项目的图像,标题和描述,但是有一个大问题:

如何在本地XML文件中解析可绘制图像以获取图像?所有图像都存储在drawable文件夹中.

示例XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<cities>
    <city>
        <cid>1</cid>
        <name>Passau</name>
        <image>@drawable/passau</image>
        <desc>Lorem ipsum</desc>
    </city>
    <city>
        <cid>2</cid>
        <name>Bamberg</name>
        <image>@drawable/bamberg</image>
        <desc>Lorem ipsum</desc>
    </city>
    <city>
        <cid>3</cid>
        <name>Augsburg</name>
        <image>@drawable/augsburg</image>
        <desc>Lorem ipsum</desc>
    </city>
</cities>

适配器示例:

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Ben on 29.07.2014.
 */
public class ListViewAdapter extends ArrayAdapter<RowItem> {
    Context context;

    public ListViewAdapter(Context context, int resourceId,
                               List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
        TextView txtDesc;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row_item, null);
            holder = new ViewHolder();
            holder.txtDesc = (TextView) convertView.findViewById(R.id.Desc);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.Titel);
            holder.imageView = (ImageView) convertView.findViewById(R.id.Bild);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtDesc.setText(rowItem.getDesc());
        holder.txtTitle.setText(rowItem.getTitle());
        holder.imageView.setImageResource(rowItem.getImageDrawable());

        return convertView;
    }
}

最佳答案 将资源名称添加到xml中并尝试以下操作:

How do I get the resource id of an image if I know its name?

从您从xml获取的资源名称获取资源ID.就这样.

基于他的适配器,这里是改变的行:

holder.imageView.setImageResource(getContext().getResources().getIdentifier(rowItem.getImage(), "drawable", getContext().getPackageName()));

当你抬头:http://developer.android.com/reference/android/widget/ArrayAdapter.html
它获取上下文工作并提供方法“getContext”,您可以访问getResources().

您的RowItem已经保存了具有图像名称的字符串.您必须删除xml中的“@ drawable /”前缀.

点赞