我"克隆"一个有两种不同策略的对象。 CloneConstructor和克隆方法。 (以下代码) 在编辑克隆的对象之后,"原始"对象也被编辑。 我做错了什么?
我的自定义对象
public class FormData {
HashMap<String, Object> values;
String stgFormInstanceId, requestId, stgFormDatenTransfer;
// Constructor for creating data with WebService
public FormData(SoapObject so, StgForm form) {....}
// Constructor for creating new data
public FormData() {....}
// clone Constructor
public FormData(FormData oldData) {
this.requestId = oldData.requestId;
this.stgFormDatenTransfer = oldData.stgFormDatenTransfer;
this.stgFormInstanceId = oldData.stgFormInstanceId;
this.values = oldData.values;
this.toString();
}
// Clone Method
public FormData cloneFormData() {
FormData newData = new FormData();
newData.requestId = this.requestId;
newData.stgFormDatenTransfer = this.stgFormDatenTransfer;
newData.stgFormInstanceId = this.stgFormInstanceId;
newData.values = this.values;
newData.toString();
return newData;
}
public void addValue(String key, Object value) {
this.values.put(key, value);
}
}
我的活动:
public class DetailActivity extends ActionBarActivity implements
ActionBar.TabListener {
private static HashMap<String, StgField> fields;
private ActionBar actionBar;
private FormData data;
private FormData editedData;
private String instanceId, requestId;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private static ViewMode viewMode;
private static ArrayList<EditText> editTextList;
private StgForm currentForm;
private Context context;
private FormData editedData2;
private FormData editedData3;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
data = ....;
}
private void editTicket() {
setViewMode(ViewMode.edit);
editedData2 = new FormData(data);
editedData3 = data.cloneFormData();
setEditabilityOfFields(fields, true);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
private void saveTabValues(int position) {
ArrayList<StgField> fields = getFieldsOfTAB(position);
for (StgField field : fields) {
View view = field.getViewItem();
if (view instanceof EditText) {
EditText et = (EditText) view;
String fieldName=field.getFieldName();
Editable value = et.getText();
String s = value.toString();
try {
editedData2.addValue(fieldName,
s);
} catch (Exception e) {}
try {
editedData3.addValue(fieldName,
s);
} catch (Exception e) {}
}
}
System.out.println(data.toString());
System.out.println("-------------");
System.out.println(editedData2.toString());
System.out.println("-------------");
System.out.println(editedData3.toString());
}
}
现在,在调用editTicket()然后在我的Activity中保存TABvalues()之后,所有三个对象 数据 / editData2 / editData3 是相同的。这对于editData2和editData3来说没问题,但我不明白,为什么&#34;数据&#34;也改变了......
感谢您的帮助
答案 0 :(得分:1)
您正在传输数据&#34;数据&#34;在公共FormData(FormData oldData);在editTicket();因此,无论何时编辑Edit2或Edit3,您都将更改数据的原始值。您需要在克隆方法中使用clone(),或者在适用的时候使用新的Object(oldObject),例如String s = new String(oldString);
编辑:
当我说&#34;转移&#34;我的意思是你只是分配对原始值的引用。所以你不是在你的公共FormData(FormData oldData);
中创建一个新的值答案 1 :(得分:0)
// clone Constructor
public FormData(FormData oldData) {
requestId = new String(oldData.requestId.toString());
stgFormDatenTransfer = new String (oldData.stgFormDatenTransfer.toString());
values = new HashMap<String,Object>(oldData.values);
}
只是没有用