2012-04-02 20 views
1

我有一個包含對象X的列表的對象X. 我在列表上做一個循環,並通過一個Intent將列表中的對象X從列表傳遞給另一個活動。在活動中傳遞複合對象引用

在其他活動中,我修改此對象X,然後再返回這個修改的對象X.

當我回到我的第一個活動對象X,我可以看到它被修改,這一點是確定的。

但是,根對象X(包含對象X的列表並來自修改的對象X的列表)不應該被修改,而且我傳遞了對該意圖的引用?

如果不是,我該如何設法修改它作爲對象X在其他對象X內部深度的根深度?

(告訴我我不夠清楚)。

感謝您的幫助。 我有發送和對象X活動通過另一個活動

我的目標是更準確(不支付的事實,這是序列化,而不是Parcelable關注,我會解決這個版本):

public class CategoryBean implements Externalizable, Cloneable { 
    public static final boolean ACTIVE = true; 
    public static final boolean INACTIVE = false; 

    public static final int VALIDATED = 1; 
    public static final int NON_OBSERVED = 2; 
    public static final int IN_PROGRESS = 3; 
    public static final int PARTIEL = 4; 

    private String perimetre; 
    private CategoryBean parentCategory; 
    private List<CategoryBean> categoryList = new ArrayList<CategoryBean>(); 
    protected String title; 
    /** 
    * Category validée ou non 
    */ 
    protected int state; 
    /** 
    * Category active ou inactive 
    */ 
    protected boolean activated = ACTIVE; 

    // public CategoryBean(Parcel in) { 
    // this.parentCategory = in.readParcelable(CategoryBean.class.getClassLoader()); 
    // this.categoryList = Arrays.asList(in.readParcelableArray(CategoryBean.class.getClassLoader())); 
    // this.title = in.readString(); 
    // } 

    /** 
    * @return the parentCategory 
    */ 
    public CategoryBean getParentCategory() { 
     return parentCategory; 
    } 

    /** 
    * @return the perimetre 
    */ 
    public String getPerimetre() { 
     return perimetre; 
    } 

    /** 
    * @param perimetre 
    *   the perimetre to set 
    */ 
    public void setPerimetre(String perimetre) { 
     this.perimetre = perimetre; 
    } 

    /** 
    * @param parentCategory 
    *   the parentCategory to set 
    */ 
    public void setParentCategory(CategoryBean parentCategory) { 
     this.parentCategory = parentCategory; 
    } 

    /** 
    * @return the category 
    */ 
    public List<CategoryBean> getCategoryList() { 
     return categoryList; 
    } 

    /** 
    * @param category 
    *   the category to set 
    */ 
    public void setCategoryList(List<CategoryBean> categoryList) { 
     this.categoryList = categoryList; 
    } 

    /** 
    * @return the title 
    */ 
    public String getTitle() { 
     return title; 
    } 

    /** 
    * @param title 
    *   the title to set 
    */ 
    public void setTitle(String title) { 
     this.title = title; 
    } 

    /** 
    * @return the state 
    */ 
    public int getState() { 
     return state; 
    } 

    /** 
    * @param state 
    *   the state to set 
    */ 
    public void setState(int state) { 
     this.state = state; 
    } 

    /** 
    * @return the activated 
    */ 
    public boolean isActivated() { 
     return activated; 
    } 

    /** 
    * @param activated 
    *   the activated to set 
    */ 
    public void setActivated(boolean activated) { 
     this.activated = activated; 
    } 

    @Override 
    public int hashCode() { 
     return parentCategory.hashCode() + categoryList.hashCode() + title.hashCode(); 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException { 
     setPerimetre((String) input.readObject()); 
     setParentCategory((CategoryBean) input.readObject()); 
     setCategoryList((List<CategoryBean>) input.readObject()); 
     setTitle((String) input.readObject()); 
     setState((Integer) input.readObject()); 
     setActivated((Boolean) input.readObject()); 
    } 

    @Override 
    public void writeExternal(ObjectOutput output) throws IOException { 
     output.writeObject(getPerimetre()); 
     output.writeObject(getParentCategory()); 
     output.writeObject(getCategoryList()); 
     output.writeObject(getTitle()); 
     output.writeObject(getState()); 
     output.writeObject(isActivated()); 
    } 

    @Override 
    public CategoryBean clone() throws CloneNotSupportedException { 
     try { 
      CategoryBean clone = (CategoryBean) super.clone(); 
      clone.setPerimetre(clone.getPerimetre()); 
      clone.setParentCategory(clone.getParentCategory()); 
      clone.setCategoryList(clone.getCategoryList()); 
      clone.setTitle(clone.getTitle()); 
      clone.setState(clone.getState()); 
      clone.setActivated(clone.isActivated()); 
      return clone; 
     } catch (CloneNotSupportedException e) { 
      throw new InternalError(); 
     } 
    } 

    @Override 
    public String toString() { 
     StringBuilder sb = new StringBuilder("(CategoryBean -> "); 
     sb.append(" perimetre : "); 
     sb.append(getPerimetre()); 
     sb.append(" parentCategory : "); 
     sb.append(getParentCategory() != null ? getParentCategory().getTitle() : null); 
     sb.append(" categoryList : "); 
     sb.append(getCategoryList()); 
     sb.append(" title : "); 
     sb.append(getTitle()); 
     sb.append(" state : "); 
     sb.append(getState()); 
     sb.append(" activated : "); 
     sb.append(isActivated()); 
     sb.append(")"); 
     return sb.toString(); 
    } 
} 
+0

定義'我有一個包含對象X的對象X' – 2012-04-02 13:21:45

+0

@Samir我已經更新了我的問題 – Nico 2012-04-02 13:27:46

回答

2

一般來說,我不會推薦通過引用做修改,即使它可能在Java中。它有幾個缺點,在開發階段稍後會變得清晰,當你有一千行代碼和一些其他開發人員在同一個項目上工作,每個人都想知道爲什麼列表中的項目會被神奇地修改,並且在哪裏:)

我建議你只需將修改後的對象存儲回列表中替換它。你所要做的就是跟蹤索引。

使用List接口的設置方法:

set(int index, Object element) 
     Replaces the element at the specified position in this list with the specified element. 

編輯: 這也將是低耦合性和模塊化方面的一個更簡潔的方法。因此,一個活動不依賴於另一個活動,並可以重複用於特定任務。

回答你的確切問題:我猜Android會克隆這個項目,或者是因爲它被複制/新創建的項目的序列化,並且因爲上面提到的原因而沒有通過引用傳遞。

+0

謝謝:)我想我會跟蹤完整的路徑。 – Nico 2012-04-02 16:12:47

+0

可以確認,如果您通過intent傳遞它們,android並不尊重您的引用。相反,如果您的對象編輯有多於1層的深度,它會複製出令人討厭的項目。 – 2015-09-02 09:47:18