我有一个Object X,它包含一个Objects X列表。 我在列表上做了一个循环,并通过Intent将一个Object X从列表传递给另一个活动。
在另一个活动中,我修改了这个Object X,然后返回这个修改过的Object X。
当我在第一次活动中找回对象X时,我可以看到它已被修改,这一点没问题。
但是根对象X(包含对象X列表并且来自修改后的对象X的那个)不应该被修改,因为我传递了对intent的引用?
如果没有,我怎么能设法修改它,因为其他Objects X中的Objects X的根深度可以很深?
(告诉我,我不够清楚)。
感谢您的帮助。 我有一个带有send和Object X的活动通过另一个活动
我的目标是更精确(不要注意它是Serializable而不是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 :(得分:2)
一般来说,我不建议通过引用进行修改,即使它可能在Java中。它有几个缺点,在开发阶段后会变得清晰,当你有数千行代码和一些其他开发人员在同一个项目上工作时,每个人都想知道为什么列表项被神奇地修改并且WHERE:)
我建议您只需将修改后的对象存储回列表中即可替换它。您所要做的就是跟踪索引。
使用List接口的set方法:
set(int index, Object element)
Replaces the element at the specified position in this list with the specified element.
编辑: 就低耦合和模块化而言,这也是一种更清洁的方法。因此,一项活动不依赖于另一项活动,可以重复用于特定任务。
要回答您的确切问题:我猜Android确实克隆了该项目,或者由于上述原因,由于项目的序列化而被复制/新创建并且未通过引用传递。