Search code examples
androidxmlimagesimpleadapter

Can set dynamic icon from XML in SimpleAdapter?


I would like to use a SimpleAdapter instead of customizing an ArrayAdapter. Everything is working except an icon that is associated with each row. The icon relates directly to the label. SO depending on the label, the icon may be different.

Here is sample XML

<profile>
<id>16</id>
<name>Random Name</name>
<site>Random URL</site>
<icon>R.drawable.random_icon</icon>
</profile>

My custom Row layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label" />

<ImageView
    android:id="@+id/arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow" />

</LinearLayout>

Now here is where I Parse my XML and set up my Adapter (edited to just relevant parts):

        NodeList children = doc.getElementsByTagName("profile");

    for (int i = 0; i < children.getLength(); i++) {

            HashMap<String, String> map = new HashMap<String, String>();

                    Element e = (Element) children.item(i);

                    map.put("id", ParseXMLMethods.getValue(e, "id"));
        map.put("name", ParseXMLMethods.getValue(e, "name"));
        map.put("site", ParseXMLMethods.getValue(e, "site"));
        map.put("icon", ParseXMLMethods.getValue(e, "icon"));
        mylist.add(map);
    }

           View header = getLayoutInflater().inflate(R.layout.header, null, false);

       ListAdapter adapter = new SimpleAdapter(this, mylist,
            R.layout.rowlayout, new String[] { "name", "icon" }, new int[] { R.id.label, R.id.icon });


    final ListView lv = getListView();
    lv.addHeaderView(header, null, false);
    lv.setSelector(R.color.list_selector);
    setListAdapter(adapter);

The program doesn't crash. Everything appears as is, I even parse the "site" so when the row is clicked is opens up a web view. I just can't get the icon to show. Is this even possible with SimpleAdapter?

UPDATE: Here is overriden getView() method:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

    int idImage = context.getResources().getIdentifier("icon","drawable", context.getPackageName());

    //   ????????

    return view;
}

Solution

  • I can recommend you to achieve this is by using getIdentifier method from resources in an extended SimpleAdapter. But for that, first you need to change your xml a little:

    Instead of saving programmable reference of image into your xml, you should simply save the file name:

    <profile>
      <id>16</id>
      <name>Random Name</name>
      <site>Random URL</site>
      <icon>random_icon</icon>
    </profile>
    

    Now, extend your SimpleAdapter to your own adapter class and override getView method. In that method you can re-construct resource id from the name and set it to your ImageView:

    int idImage = context.getResources().getIdentifier("nameOfResource", "drawable", context.getPackageName());
    

    Update

    public class MyAdapter extends SimpleAdapter {
    
        private Context context;
    
        List<HashMap<String, String>> lstData = new ArrayList<HashMap<String,String>>();
    
        public MyAdapter(Context context, List<HashMap<String, String>> items) {
            super(context, items, android.R.id.text1, R.layout.rowlayout, new String[] { "name", "icon" }, new int[] { R.id.label, R.id.icon });
    
            this.context = context;
            lstData = items;
        }
    
    
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
    
            //smart initialization      
            if(convertView == null){
                LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflator.inflate(R.layout.rowlayout, parent, false);
    
                holder = new ViewHolder();
                holder.title = (TextView) convertView.findViewById(R.id.label);
                holder.img = (ImageView) convertView.findViewById(R.id.icon);
                convertView.setTag(holder);
            }
            else
                holder = (ViewHolder) convertView.getTag();
    
            //get item
            HashMap<String, String> map = lstData.get(position);
    
            //setting title
            holder.title.setText(map.get("name"));
    
            int idImage = context.getResources().getIdentifier(map.get("icon"),"drawable", context.getPackageName());
            holder.img.setImageResource(idImage);
    
            return convertView;
        }
    
    
    
        //this is better approach as suggested by Google-IO for ListView
        private static class ViewHolder{
            TextView title;
            ImageView img;
        }
    
    }
    

    *this class code is a reference for you to see how your adapter should look like. The more appropriate way would be to use ArrayAdapter, but since you are already using SimpleAdapter so I've just extended its capabilities