I’ve got this java code and I’ve two questions:
-
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.
-
What did I do to start an activity when I press, for example, on the item called “Apple”?
How can I solve my problem? Thank you very much.
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<Drawable> 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<Drawable>();
albumCovers.add(photo);
albumCovers.add(photo2);
// The following code simply generates the Expandable Lists content (Strings)
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
Map<String, String>
//List A
curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "A");
curGroupMap.put(SURNAME, "(2 Photos)");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Apple");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Astro");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
childData.add(children);
//List B
curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "B");
curGroupMap.put(SURNAME, "(2 Photos)");
children = new ArrayList<Map<String, String>>();
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Bee");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Bomb");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
childData.add(children);
//List C
curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "C");
curGroupMap.put(SURNAME, "(0 Photo)");
children = new ArrayList<Map<String, String>>();
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<? extends List<? extends Map<String, ?>>> mChildData;
private String[] mChildFrom;
private int[] mChildTo;
public MyExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData, int groupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> 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<String, ?> data, String[] from, int[] to, int groupPosition, int childPosition) {
int len = to.length - 1;
// Apply TextViews
for (int i = 0; i < 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));
}
}
}
}
}
The line
in your
bindViewmethod sets the image for the child view. You can set it to whatever you want, can make it data specific (say you have an image url in the data represented by this view, you can set theimgVto display that image.To attach a
View.OnClickListenerto every child item, you simply set it either in the overriddengetChildView(forv/convertView) or yourbindViewmethod (forview):Update 1
if you certainly have a drawable defined with the name that is shown in your list, you can update your for cycle as:
just update the
yourdefaultpackagepart with your application’s default package.Update 2
Below is a detailed sample for how to proceed when you’d like to have different icons for all of the child rows in an expandable list view based on the data the row holds:
Let’s assume the hierarchical data structure to show is:
res/raw/data.xml:
You need to define your classes to represent this data as java objects. It’s simple, basically you need a
Groupthat has aletterand alist of Child, whereChildhas anameand animage.So you’ll have a
Group.javaand aChild.javaclass with the properties above:Group.java:
Child.java:
To read the xml structure into an ArrayList of
Groupobjects, you will need a simple xml handler too, which will examine the tags, and build up the structure you need:XmlHandler.java:
For displaying a list / expandable list, you need an
Adapterclass. In this case we’ll use a simpleBaseExpandableListAdapterimplementation to show the parent and child rows with their proper icons:MyExpandableListAdapter.java:
Finally, the activity class, which uses the above three:
testactivity.java:
I hope you can work it out this way.