4

I would like to parse a local XML file from the assets folder in android and I have found a nice tutorial: http://theopentutorials.com/tutorials/android/xml/android-simple-xmlpullparser-tutorial/

All works great, I´ve converted the tutorial code to get a nice listview with an image, title and description per row item, but there´s one big problem:

How can I parse a drawable image within a local XML file to get the image loading? All images are stored at drawable folder.

Example 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>

Adapter Example:

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;
    }
}
ben
  • 161
  • 3
  • 18

2 Answers2

2

Add the names of your resources into xml and try this: How do I get the resource id of an image if I know its name? To get the resource id from the resource name you got from xml. Thats all.

Based in his adapter here is the line to change:

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

When you look up: http://developer.android.com/reference/android/widget/ArrayAdapter.html It gets the context to work in and provides method "getContext" over this you get access to getResources().

Your RowItem holds already the string with the name of the image. You have to remove the "@drawable/" prefix in your xml.

Community
  • 1
  • 1
Rene M.
  • 2,660
  • 15
  • 24
  • Thank you. Don´t know how to implement this. I´ve an custom adapter class, a parser class and my data defining class. I´ve to use String as type at my defining class and to parse an integer at my adapter class for holding the imageview, but where I´ve to set this: `getResources().getIdentifier(mDrawableName , "drawable", getPackageName());` – ben Jul 29 '14 at 14:02
  • 1
    In your adapter where you have the context to access resources. Past your adapter I will provide example. – Rene M. Jul 29 '14 at 14:04
  • Thanks, I´ve added the adapter as edit of my question. – ben Jul 29 '14 at 14:09
  • sorry for the delay, but I was at the supermarket when you posted the adapter code ;) – Rene M. Jul 29 '14 at 14:27
  • Thanks a lot. I only have got an error cannot resolve getpackagename(). – ben Jul 29 '14 at 14:37
  • 1
    I changed the example, my fault. getPackageName() is part of context ;) so you need to getContext().getPackageName() or hardcode the packagename you used in your manifest. – Rene M. Jul 29 '14 at 14:40
  • I´ve figured it out. context.getpackename() and all is working. Thank you very very much!! – ben Jul 29 '14 at 14:44
0

Here i will consider that you already have cities as collection class. You can do this by collecting the image tag as below.

 else if (tagname.equalsIgnoreCase("image")) {
                    city.setImageDrawable(text);

So city object will have this image value from XML tag. While inflating your views in your adapter just get this value from the city object and set it to your imageview like -

Get the image drawable value set in the city object like-

Drawable imageDrawable = city.getImageDrawablwe();

Set the image to your image view in your adapter getView method.

setBackgroundResource(imageDrawable);
  • Thank, but it doesn´t really work, cause the drawable data typ can´t be handled at the else if statement. Or I´m doing anything wrong... – ben Jul 29 '14 at 14:05
  • I did not run this code. You have to do something like this only. Just check how you can get the drawable element from the xml and set in cities object. – Devendra B. Singh Jul 29 '14 at 14:19