我创建了一个带有 subMenu 的菜单,并尝试在每个最后一个事件上设置一个事件。
除了子菜单的最后一次迭代(每个子菜单可以有或没有子菜单,它是随机的)之外,它运行良好。
我添加了 Sys.out 以显示每个子菜单迭代的 itemId 和 ItemTitle,它在最后一次迭代(第四个子菜单)之外的所有子菜单中都有效
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 the activity is 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 a é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;
}
}
编辑
日志猫:
05-15 09:03:37.268: I/Choreographer(794): Skipped 295 frames! The application may be doing too much work on its main thread.
05-15 09:03:37.749: I/Choreographer(794): Skipped 50 frames! The application may be doing too much work on its 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! The application may be doing too much work on its main thread.
05-15 09:03:48.829: I/Choreographer(794): Skipped 338 frames! The application may be doing too much work on its 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! The application may be doing too much work on its main thread.
05-15 09:04:00.269: I/Choreographer(794): Skipped 385 frames! The application may be doing too much work on its 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! The application may be doing too much work on its main thread.
05-15 09:04:04.669: I/Choreographer(794): Skipped 260 frames! The application may be doing too much work on its main thread.
05-15 09:04:05.849: I/Choreographer(794): Skipped 35 frames! The application may be doing too much work on its main thread.
05-15 09:04:06.439: I/Choreographer(794): Skipped 58 frames! The application may be doing too much work on its main thread.