Android - How to set a different icon for every child of an ExpandableListView
<p>I've got this java code and I've two questions:</p>

<ol>
<li><p>I would like to set a different icon for every child of every parent, but with this code I can only set the same icon for all child or an icon for a child situated in a X place and another icon for the other child. </p></li>
<li><p>What did I do to start an activity when I press, for example, on the item called "Apple"?</p></li>
</ol>

<p>How can I solve my problem? Thank you very much.</p>

<pre><code>public class sedactivity extends ExpandableListActivity { 
    ExpandableListAdapter mAdapter;
    private final static String NAME = "NAME";
    private final static String SURNAME = "SURNAME";
    private Resources res;
    private Drawable photo, photo2, photo3, photo4;
    private List&lt;Drawable&gt; albumCovers;


    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_main);

            // Create a List of Drawables to insert into the Expandable List
            // This can be completely dynamic
            res = this.getResources();
            photo = (Drawable) res.getDrawable(R.drawable.bee);
            photo2 = (Drawable) res.getDrawable(R.drawable.astro);
            photo3 = (Drawable) res.getDrawable(R.drawable.bomb);
            photo4 = (Drawable) res.getDrawable(R.drawable.apple);
            albumCovers = new ArrayList&lt;Drawable&gt;();
            albumCovers.add(photo);
            albumCovers.add(photo2);

            // The following code simply generates the Expandable Lists content (Strings)
            List&lt;Map&lt;String, String&gt;&gt; groupData = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
            List&lt;List&lt;Map&lt;String, String&gt;&gt;&gt; childData = new ArrayList&lt;List&lt;Map&lt;String, String&gt;&gt;&gt;();
            Map&lt;String, String&gt;


            //List A
            curGroupMap = new HashMap&lt;String, String&gt;();
            groupData.add(curGroupMap);

            curGroupMap.put(NAME, "A");
            curGroupMap.put(SURNAME, "(2 Photos)");



            List&lt;Map&lt;String, String&gt;&gt; children = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
            Map&lt;String, String&gt; curChildMap = new HashMap&lt;String, String&gt;();
            children.add(curChildMap);


            curChildMap.put(NAME, "Apple");


            curChildMap = new HashMap&lt;String, String&gt;();
            children.add(curChildMap);

            curChildMap.put(NAME, "Astro");

            curChildMap = new HashMap&lt;String, String&gt;();
            children.add(curChildMap);


            childData.add(children);


            //List B
            curGroupMap = new HashMap&lt;String, String&gt;();
            groupData.add(curGroupMap);


            curGroupMap.put(NAME, "B");
            curGroupMap.put(SURNAME, "(2 Photos)");

            children = new ArrayList&lt;Map&lt;String, String&gt;&gt;();

            curChildMap = new HashMap&lt;String, String&gt;();
            children.add(curChildMap);

            curChildMap.put(NAME, "Bee");

            curChildMap = new HashMap&lt;String, String&gt;();
            children.add(curChildMap);

            curChildMap.put(NAME, "Bomb");

            curChildMap = new HashMap&lt;String, String&gt;();
            children.add(curChildMap);


            childData.add(children);


            //List C
            curGroupMap = new HashMap&lt;String, String&gt;();
            groupData.add(curGroupMap);


            curGroupMap.put(NAME, "C");
            curGroupMap.put(SURNAME, "(0 Photo)");

            children = new ArrayList&lt;Map&lt;String, String&gt;&gt;();


            childData.add(children);

            // Set up our adapter
            mAdapter = new MyExpandableListAdapter(
                            this,
                            groupData,
                            R.layout.list_parent,
                            new String[] { NAME, SURNAME },
                            new int[] { R.id.rowText1, R.id.rowText2,R.id.photoAlbumImg },
                            childData,
                            R.layout.list_child,
                            new String[] { NAME, SURNAME },
                            new int[] { R.id.rowText1, R.id.photoAlbumImg }
                    );
            setListAdapter(mAdapter);
            registerForContextMenu(getExpandableListView());
    }


    /**
     * A simple adapter which allows you to bind data to specific
     * Views defined within the layout of an Expandable Lists children
     * (Implement getGroupView() to define the layout of parents)
     */


    public class MyExpandableListAdapter extends SimpleExpandableListAdapter {

            private List&lt;? extends List&lt;? extends Map&lt;String, ?&gt;&gt;&gt; mChildData;
            private String[] mChildFrom;
            private int[] mChildTo;

            public MyExpandableListAdapter(Context context,
                            List&lt;? extends Map&lt;String, ?&gt;&gt; groupData, int groupLayout,
                            String[] groupFrom, int[] groupTo,
                            List&lt;? extends List&lt;? extends Map&lt;String, ?&gt;&gt;&gt; childData,
                            int childLayout, String[] childFrom, int[] childTo) {
                    super(context, groupData, groupLayout, groupFrom, groupTo,
                          childData, childLayout, childFrom, childTo);

                    mChildData = childData;
                    mChildFrom = childFrom;
                    mChildTo = childTo;

            }

            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {

            View v;
            if (convertView == null) {
                    v = newChildView(isLastChild, parent);
            } else {
                    v = convertView;
            }
            bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom,
                            mChildTo, groupPosition, childPosition);
            return v;

            }

         // This method binds my data to the Views specified in the child xml layout
            private void bindView(View view, Map&lt;String, ?&gt; data, String[] from, int[] to, int groupPosition, int childPosition) {
                    int len = to.length - 1;
                    // Apply TextViews
                    for (int i = 0; i &lt; len; ++i) {
                            TextView v = (TextView) view.findViewById(to[i]);
                            if (v != null) {
                                    v.setText((String) data.get(from[i]));
                            }
                            // Apply ImageView
                            ImageView imgV = (ImageView) view.findViewById(to[1]);
                            if (imgV != null) {
                                if(childPosition % 1 == 0) imgV.setImageDrawable(albumCovers.get(0));
                                else imgV.setImageDrawable(albumCovers.get(1));
                        }


                    }

            }
    }
}
</code></pre>
