i've got java code , i've 2 questions:
i set different icon every child of every parent, code can set same icon child or icon child situated in x place , icon other child.
what did start activity when press, example, on item called "apple"?
how can solve problem? thank 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 list of drawables insert expandable list // can 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); // following code generates 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 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 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()); } /** * simple adapter allows bind data specific * views defined within layout of expandable lists children * (implement getgroupview() define 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; } // method binds data views specified in 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 (int = 0; < 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
imgv.setimagedrawable(albumcovers.get(1))
in bindview
method sets image child view. can set whatever want, can make data specific (say have image url in data represented view, can set imgv
display image.
to attach view.onclicklistener
every child item, set either in overridden getchildview
(for v/convertview
) or bindview
method (for view
):
view.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo: implement tasks done here } });
update 1
if have drawable defined name shown in list, can update cycle as:
for (int = 0; < len; ++i) { textview v = (textview) view.findviewbyid(to[i]); if (v != null) { v.settext((string) data.get(from[i])); } imageview imgv = (imageview) view.findviewbyid(to[1]); imgv.setimageresource(getresources().getidentifier((string) data.get(from[i]), "drawable", "yourdefaultpackage")); } }
just update yourdefaultpackage
part application's default package.
update 2
below detailed sample how proceed when you'd have different icons of child rows in expandable list view based on data row holds:
let's assume hierarchical data structure show is:
res/raw/data.xml:
<?xml version="1.0" encoding="utf-8"?> <data> <group letter="a"> <child name="american bull" img="american_bull" /> <child name="arco's" img="arcos" /> </group> <group letter="b"> <child name="barter" img="barter" /> <child name="bitter b." img="bitter_b" /> <child name="bull hunter" img="bull_hunter" /> <child name="bull in cuba" img="bull_in_cuba" /> <child name="bullito" img="bullito" /> </group> <group letter="c"> <child name="cackle" img="cackle" /> <child name="cheapstake" img="cheapstake" /> <child name="checco's bull" img="checcos_bull" /> <child name="cheeriness" img="cheeriness" /> <child name="cross current" img="cross_current" /> <child name="cruel" img="cruel" /> </group> <group letter="d"></group> <group letter="e"></group> <group letter="f"> <child name="firearm" img="firearm" /> <child name="first of all" img="first_of_all" /> <child name="first apple" img="first_apple" /> </group> <group letter="g"> <child name="golden" img="golden" /> <child name="green bull" img="green_bull" /> </group> <group letter="h"> <child name="hollywood passion" img="hollywood_passion" /> </group> <group letter="i"></group> <group letter="j"></group> <group letter="k"> <child name="kind bull" img="kind_bull" /> <child name="kingling" img="kingling" /> </group> <group letter="l"> <child name="lab" img="lab" /> <child name="leary" img="leary" /> <child name="leg pull" img="leg_pull" /> <child name="long bull" img="long_bull" /> <child name="lucy" img="lucy" /> </group> <group letter="m"> <child name="mayla" img="mayla" /> </group> <group letter="n"> <child name="new red bull fantasy" img="new_red_bull_fantasy" /> <child name="nu energy" img="nu_energy" /> </group> <group letter="o"></group> <group letter="p"> <child name="passion" img="passion" /> <child name="power" img="power" /> </group> <group letter="q"></group> <group letter="r"> <child name="rebellion red peach" img="rebellion_red_peach" /> <child name="red bulloska" img="red_bulloska" /> <child name="red 28" img="red_28" /> <child name="red bull wallbanger" img="red_bull_wallbanger" /> <child name="red cell" img="red_cell" /> <child name="red hothead" img="red_hothead" /> <child name="red lime" img="red_lime" /> </group> <group letter="s"> <child name="speedy" img="speedy" /> <child name="spritz power" img="spritz_power" /> <child name="stiff-necked" img="stiff_necked" /> </group> <group letter="t"></group> <group letter="u"></group> <group letter="v"> <child name="vedi o' mare come e' bull" img="vedi_o_mare_come_e_bull" /> </group> <group letter="w"></group> <group letter="x"></group> <group letter="y"></group> <group letter="z"></group> <group letter="#"></group> </data>
you need define classes represent data java objects. it's simple, need group
has letter
, list of child
, child
has name
, image
.
you'll have group.java
, child.java
class properties above:
group.java:
public class group { private string letter; private arraylist<child> children; /** * @return letter */ public string getletter() { return letter; } /** * @param letter letter set */ public void setletter(string letter) { this.letter = letter; } /** * @return children */ public arraylist<child> getchildren() { return children; } /** * @param children children set */ public void setchildren(arraylist<child> children) { this.children = children; } }
child.java:
public class child { private string name; private string image; /** * @return name */ public string getname() { return name; } /** * @param name name set */ public void setname(string name) { this.name = name; } /** * @return image */ public string getimage() { return image; } /** * @param image image set */ public void setimage(string image) { this.image = image; } }
to read xml structure arraylist of group
objects, need simple xml handler too, examine tags, , build structure need:
xmlhandler.java:
import java.util.arraylist; import org.xml.sax.attributes; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler; public class xmlhandler extends defaulthandler { /** * tag- , attribute names in xml data */ private static final string tag_group = "group"; private static final string tag_child = "child"; private static final string attr_letter = "letter"; private static final string attr_name = "name"; private static final string attr_img = "img"; /** * holds processing group instance */ private group currentgroup; /** * holds processing child instance */ private child currentchild; /** * list of group objects parsed xml */ private arraylist<group> records = null; /** * @return list of group parsed xml */ public arraylist<group> getgroups() { return records; } @override public void startdocument() throws saxexception { super.startdocument(); this.records = new arraylist<group>(); } @override public void startelement(final string uri, final string localname, final string qname, final attributes attributes) throws saxexception { if (localname == null) return; if (localname.equals(tag_group))//examining group tag { currentgroup = new group(); currentgroup.setletter(attributes.getvalue(attr_letter));//the letter attribute currentgroup.setchildren(new arraylist<child>()); records.add(currentgroup); } else if (localname.equals(tag_child))//examining child tag { currentchild = new child(); currentchild.setname(attributes.getvalue(attr_name));//the name attribute currentchild.setimage(attributes.getvalue(attr_img));//the img attribute currentgroup.getchildren().add(currentchild); } } }
for displaying list / expandable list, need adapter
class. in case we'll use simple baseexpandablelistadapter
implementation show parent , child rows proper icons:
myexpandablelistadapter.java:
import java.util.arraylist; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.imageview; import android.widget.textview; public class myexpandablelistadapter extends baseexpandablelistadapter { private final layoutinflater inflater; private final arraylist<group> groups; public myexpandablelistadapter(layoutinflater inflater, final arraylist<group> groups) { this.inflater = inflater; this.groups = groups; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parentview) { final group parent = groups.get(groupposition); if (convertview == null) //if row null, create (by inflation) convertview = inflater.inflate(r.layout.list_parent, parentview, false); // display letter of group: ((textview) convertview.findviewbyid(r.id.rowtext1)). settext(parent.getletter()); // display children count of group: ((textview) convertview.findviewbyid(r.id.rowtext2)). settext("(" + parent.getchildren().size() + " ricette)"); return convertview; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parentview) { final group group = groups.get(groupposition); final child child = group.getchildren().get(childposition); if (convertview == null)//if child row null, inflate convertview = inflater.inflate(r.layout.list_child, parentview, false); // display name of child on row: ((textview) convertview.findviewbyid(r.id.rowtext1)).settext(child.getname()); // set proper icon of child on child row: ((imageview) convertview.findviewbyid(r.id.photoalbumimg)). setimageresource(parentview.getresources(). getidentifier(child.getimage(), "drawable", "com.test.com")); return convertview; } @override public object getchild(int groupposition, int childposition) { return groups.get(groupposition).getchildren().get(childposition); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public int getchildrencount(int groupposition) { return groups.get(groupposition).getchildren().size(); } @override public object getgroup(int groupposition) { return groups.get(groupposition); } @override public int getgroupcount() { return groups.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public void notifydatasetchanged() { super.notifydatasetchanged(); } @override public boolean isempty() { return ((groups == null) || groups.isempty()); } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } @override public boolean hasstableids() { return true; } @override public boolean areallitemsenabled() { return true; } }
finally, activity class, uses above three:
testactivity.java:
import java.util.arraylist; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import android.app.expandablelistactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.expandablelistadapter; /** * demonstrates expandable lists using custom {...@link expandablelistadapter} * {...@link baseexpandablelistadapter}. */ public class testactivity extends expandablelistactivity { expandablelistadapter madapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list_main); // next line removes arrow expandable list's group rows, // indicates whether group expanded or not. // i've removed, because way looks nicer. getexpandablelistview().setgroupindicator(null); // read in list of groups xml data final arraylist<group> groups = readgroupsfromxml(); // set our adapter madapter = new myexpandablelistadapter(getlayoutinflater(), groups); setlistadapter(madapter); registerforcontextmenu(getexpandablelistview()); } /** * parses data.xml file located in /res/raw/ folder, * , builds list of group. * @return list of group instances display in list */ public arraylist<group> readgroupsfromxml() { try { // initialize our handler final xmlhandler handler = new xmlhandler(); // instantiate saxparser parsing xml document final saxparser sp = saxparserfactory.newinstance().newsaxparser(); // parse /res/raw/data.xml file // (referenced r.raw.data) using our handler sp.parse(getapplicationcontext().getresources(). openrawresource(r.raw.data), handler); // return list of group built inside handler return handler.getgroups(); } catch (exception e) { log.e("error", "xml", e); } return null; } // creo il menu @override public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu, menu); return true; } // creo le azioni per tasti del menu @override public boolean onoptionsitemselected(menuitem item) { super.onoptionsitemselected(item); switch (item.getitemid()) { case r.id.home: return true; case r.id.cerca: return true; case r.id.glossario: // inserire azione return true; default: return super.onoptionsitemselected(item); } } }
i hope can work out way.
Comments
Post a Comment