android - SubMenu getItem don't work -
i have created menu submenu , try set event on each last event.
it worked excepted last itération of submenu(each submenu can have submenu or not, it's random).
i've add sys.out show itemid , itemtitle each submenu iteration, worked submenu exepted last iteration (fourth submenu)
public class tab3 extends activity { layoutinflater inflater; xmlparser parser = new xmlparser(); arraylist<hashmap<string, string>> menubase=menutest.menubase; arraylist<hashmap<string, string>> menu1=menutest.menu1; arraylist<hashmap<string, string>> menu2=menutest.menu2; arraylist<hashmap<string, string>> menu3=menutest.menu3; document doc3=menutest.doc3; arraylist<integer> idfin; static final string key_category_2="category_2"; static final string key_name_3="name_3"; static final string key_category_4="category_4"; static final string key_name_5="name_5"; static final string key_category_6="category_6"; static final string key_name_7="name_7"; static final string key_category_8="category_8"; static final string key_name_9="name_9"; static final string key_id_3="id_3"; static final string key_id_5="id_5"; static final string key_id_7="id_7"; static final string key_id_9="id_9"; private viewswitcher viewswitcher; button categorie; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //viewswitcher = new viewswitcher(tab3.this); //viewswitcher.addview(viewswitcher.inflate(tab3.this, r.layout.splash_screen, null)); setcontentview(r.layout.onglet3); categorie=(button)findviewbyid(r.id.btnmenu); categorie.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { openoptionsmenu(); } }); } //méthode qui se déclenchera lorsque vous appuierez sur le bouton menu du téléphone public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.layout.menu, menu); nodelist nodes0 = doc3.getelementsbytagname(key_category_2); nodelist nodes1 = doc3.getelementsbytagname(key_category_4); nodelist nodes2 = doc3.getelementsbytagname(key_category_6); int sum0=0; int sum1=0; int sum2=0; idfin=new arraylist<integer>(); for(int i=0;i<menubase.size();i++){ submenu x0=menu.addsubmenu(0, integer.parseint(menubase.get(i).get(key_id_3).tostring()), 0,changetoregularform(menubase.get(i).get(key_name_3).tostring())); int count0 = ((element)(nodes0.item(i))).getelementsbytagname(key_category_4).getlength(); if(count0==0){ idfin.add(integer.parseint(menubase.get(i).get(key_id_3).tostring())); }else{ for(int y0=sum0;y0<sum0+count0;y0++){ submenu x1=x0.addsubmenu(1, integer.parseint(menu1.get(y0).get(key_id_5).tostring()), 0,changetoregularform(menu1.get(y0).get(key_name_5).tostring())); int count1 = ((element)(nodes1.item(y0))).getelementsbytagname(key_category_6).getlength(); if(count1==0){ idfin.add(integer.parseint(menu1.get(y0).get(key_id_5).tostring())); }else{ for(int y1=sum1;y1<sum1+count1;y1++){ submenu x2=x1.addsubmenu(2, integer.parseint(menu2.get(y1).get(key_id_7).tostring()), 0, changetoregularform(menu2.get(y1).get(key_name_7).tostring())); int count2 = ((element)(nodes2.item(y1))).getelementsbytagname(key_category_8).getlength(); if(count2==0){ idfin.add(integer.parseint(menu2.get(y1).get(key_id_7).tostring())); } else{ for(int y2=sum2;y2<sum2+count2;y2++){ idfin.add(integer.parseint(menu3.get(y2).get(key_id_9).tostring())); system.out.println(integer.parseint(menu3.get(y2).get(key_id_9).tostring())); x2.addsubmenu(3, integer.parseint(menu3.get(y2).get(key_id_9).tostring()), 0,changetoregularform(menu3.get(y2).get(key_name_9).tostring())); } } sum2+=count2; } } sum1+=count1; } } sum0+=count0; } return true; } //méthode qui se déclenchera au clic sur un item public boolean onoptionsitemselected(menuitem item) { //on regarde quel item été cliqué grâce à son id et on déclenche une action system.out.println(item.getitemid()); system.out.println(item.gettitle()); if(idfin.contains(item.getitemid())){ intent shopentry= new intent(getapplicationcontext(),shoplayout.class); shopentry.putextra("idproduct", ""+item.getitemid()); system.out.println(item.getitemid()); startactivity(shopentry); } return false;} public static string getcharacterdatafromelement(element e) { node child = e.getfirstchild(); if (child instanceof characterdata) { characterdata cd = (characterdata) child; return cd.getdata(); } return ""; } private string changetoregularform(string s){ if(s.contains("é")){ s=s.replace("é", "é"); } if(s.contains("â")){ s=s.replace("â", "â"); } if(s.contains("è")){ s=s.replace("è", "è"); } if(s.contains("ê")){ s=s.replace("ê", "ê"); } if(s.contains("ë")){ s=s.replace("ë", "ë"); } if(s.contains("î")){ s=s.replace("î", "î"); } if(s.contains("ï")){ s=s.replace("ï", "ï"); } if(s.contains("ô")){ s=s.replace("ô", "ô"); } if(s.contains("ö")){ s=s.replace("ö", "ö"); } if(s.contains("ù")){ s=s.replace("ù", "ù"); } if(s.contains("û")){ s=s.replace("û", "û"); } if(s.contains("ü")){ s=s.replace("ü", "ü"); } if(s.contains("ç")){ s=s.replace("ç", "ç"); } if(s.contains("Å")){ s=s.replace("Å", "œ"); } if(s.contains("â¬")){ s=s.replace("â¬", "€"); } if(s.contains("Ã")){ s=s.replace("Ã", "à"); } return s; } }
edit
logcat:
05-15 09:03:37.268: i/choreographer(794): skipped 295 frames! application may doing work on main thread. 05-15 09:03:37.749: i/choreographer(794): skipped 50 frames! application may doing work on main thread. 05-15 09:03:47.069: i/system.out(794): 2 05-15 09:03:47.069: i/system.out(794): cosmétiques 05-15 09:03:47.488: i/choreographer(794): skipped 122 frames! application may doing work on main thread. 05-15 09:03:48.829: i/choreographer(794): skipped 338 frames! application may doing work on main thread. 05-15 09:03:58.331: i/system.out(794): 180 05-15 09:03:58.399: i/system.out(794): maquillage 05-15 09:03:58.769: i/choreographer(794): skipped 146 frames! application may doing work on main thread. 05-15 09:04:00.269: i/choreographer(794): skipped 385 frames! application may doing work on main thread. 05-15 09:04:03.291: i/system.out(794): 1024 05-15 09:04:03.330: i/system.out(794): yeux 05-15 09:04:03.639: i/choreographer(794): skipped 118 frames! application may doing work on main thread. 05-15 09:04:04.669: i/choreographer(794): skipped 260 frames! application may doing work on main thread. 05-15 09:04:05.849: i/choreographer(794): skipped 35 frames! application may doing work on main thread. 05-15 09:04:06.439: i/choreographer(794): skipped 58 frames! application may doing work on main thread.
Comments
Post a Comment