2012-04-02 2 views
1

객체 X의 목록을 포함하는 객체 X가 있습니다. 목록에서 루프를 수행하고 목록에서 객체 X를 인 텐트를 통해 다른 활동으로 전달합니다.활동을 통해 complexe 객체 참조 전달하기

다른 활동에서, 나는이 개체 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가있다. ' –

+0

@Samir 나는 내 질문을 업데이트했다. – Nico

답변

2

일반적으로 자바로 가능하더라도 참조로 수정하는 것은 좋지 않습니다. 그것은 몇 가지 단점을 가지고 있습니다. 개발 단계에서 나중에 코드의 라인 수가 많고 같은 프로젝트에서 일하는 몇 명의 개발자가있을 때 모든 사람들이 왜 목록 항목이 마술처럼 왜 수정되는지 궁금합니다.

수정 된 개체를 다시 목록에 다시 저장하는 것이 좋습니다. 색인을 추적하면됩니다.

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

편집 : 또한 낮은 커플 링과 모듈 방식의 측면에서 청소기 방법이 될 것입니다

는 List 인터페이스의 설정 방법을 사용합니다. 그래서 하나의 활동은 다른 활동에 의존하지 않고 특정 작업을 위해 재사용 될 수 있습니다.

정확한 질문에 답하기 : Android가 항목을 복제했거나 복사 한 항목/새로 생성 한 항목의 일련 번호로 인해 위에서 언급 한 이유 때문에 참조로 전달되지 않은 것 같습니다.

+0

감사합니다. 나는 완전한 경로를 추적 할 것입니다. – Nico

+0

인 텐트를 통해 전달한 경우 Android가 참조를 존중하지 않음을 확인할 수 있습니다. 대신, 그것은 당신이 객체 편집에 깊이가 1 층 이상이라면 다소 성가신 항목을 복제합니다. –

관련 문제