2015-11-10 46 views
0

嘿傢伙,所以我正在做這雙循環。因此,在第一個for循環中,我通過一對imageButton進行循環。第二個for循環im循環字符串值(字符串,如「食物」,「酒吧」,「體育」)。如果第二個數組的第一個元素是單詞「food」,並且第二個數組的第二個單詞是「bar」,那麼我想要做的就是將第一個imageButton設置爲食物圖標」。這是我得到的。它目前所做的是將所有imageButtons設置爲「食物圖標」。雙循環與ImageButtons?

for(ImageButton button : iconArray) 
    { 
     for(int i = 0; i < tags.size(); i ++) 
     { 
      if(tags.get(i) == "food") 
      { 
       button.setImageResource(R.drawable.small_icon_food); 
       break; 
      } 
      else if(tags.get(i) == "bar") 
      { 
       button.setImageResource(R.drawable.small_icon_bar); 
       break; 
      } 

       etc.. 


    } 

回答

2

你的循環是不合理的,它會讓你的按鈕總是用過去的標籤。請糾正這樣的問題

for(int i = 0; i < iconArray.length; i ++) 
{ 
    ImageButton button = iconArray[i]; 
    if(tags.get(i) == "food") 
    { 
     button.setImageResource(R.drawable.small_icon_food); 
     break; 
    } 
    else if(tags.get(i) == "bar") 
    { 
     button.setImageResource(R.drawable.small_icon_bar); 
     break; 
    } 

     etc.. 

} 
+0

對不起有一個編碼的大腦放屁..感謝解決方案的人。 – TheQ