2014-03-05 105 views
0

我在更新arraylist中的元素時遇到問題。我有ViewLabelINScreen類,我構建了這個類的元素的數組列表,但是當我更新這個數組列表中的元素時,同一個對象中的另一個元素的值爲Default。我怎樣才能改變這個元素而無需改變其他元素。更新ArrayList中的元素

我改變了這個元素在不同的類中添加這個元素,我需要這個數組列表static。

ViewLabelINScreen:

public class ViewLabelINScreen { 
    public int xP; 
    public int yP; 
    public String nameLabel; 
    public int stringWidth4; 
    public int fontHeight; 
    public boolean stored; 

    public ViewLabelINScreen(int XP,int YP,String NameLabel,int StringWidth4 ,int FontHeight) 
    { 
     xP=XP; yP=YP; nameLabel=NameLabel;stringWidth4=StringWidth4;fontHeight=FontHeight; 
    } 

    //this contractor for change namelabel 
    public ViewLabelINScreen (String Namelabel) 
    { 
     nameLabel=Namelabel; 
    } 

    public ViewLabelINScreen() { 

    }  
} 

下面的類添加元素進入數組列表。

public class WgsGrid { 
    //Added by A.B 
    MapSelection mapSelection; 
    public static ArrayList<ViewLabelINScreen> INFOLable = new ArrayList<ViewLabelINScreen>(); 
    for (int i4 = hMin; i4 <= hMax; i4++) 
    { 
     INFOLable.add(new ViewLabelINScreen(labelRectX, labelRectY - yOffset4, SetLabels.getYlabels()[counter2],stringWidth4,fontHeight)); 
    } 
} 

該類NeedChangeArrayList更改元素:

class NeedChangeArrayList { 
    public void ChangeAraay() 
    { 
     if (WgsGrid.INFOLable.isEmpty() != true) 
     { 
      for (int s = 0; s < ylabels.length; s++) 
      { 
       WgsGrid.INFOLable.set(s, new ViewLabelINScreen(ylabels[s])); 
      } 

      //ylabels[s] string array get value from array and put in nameLabel 
     } 
    } 
} 
+0

你實例化一個'WgsGrid'對象? – Coderchu

+0

是的,我啓動程序時,實例化WgsGrid時,將所有元素內ArrayList。 – user61301

+0

其他時候當用戶點擊按鈕時,程序調用「ChangeAraay」更新元素名稱標籤裏面的內容。 – user61301

回答

0

你改變數組列表中的所有nameLabel值?如果是這樣,你可以在for循環中試試這個:

for(int s =0; s< WgsGrid.INFOLable.size();s++){ 
    WgsGrid.INFOLable.get(s).setNameLabel("whatever"); 
} 

你需要爲nameLabel創建一個setter方法。

public void setNameLabel(String nameLabel){ 
    this.nameLabel = nameLabel; 
} 

(我已經從ylabels.length改爲ArrayList的大小。不知道你用ylabels做)

+0

非常感謝您幫助我,滿足我的需求。 – user61301