提问人:Nummer Eins 提问时间:5/29/2021 更新时间:5/29/2021 访问量:26
Listviewitem 按引用传递,而不是按值传递(可解析和可序列化)
Listviewitem is passed by reference rather by value (parceable and serializable)
问:
我有一个简单的 listView,它调用另一个片段来更新选定的驱动程序:
public static ArrayList<Driver> allDrivers = new ArrayList<>(); //statict just so i can checj if the value is automatically changed in the other fragment, which is unfourtantly the case
DriverListAdapter adapter;
ListView listViewDriver;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final DriverUpdateFragment fragment = new DriverUpdateFragment();
Driver selectedDriver2 = (Driver) parent.getItemAtPosition(position);
Fragment ft = getFragmentManager().findFragmentByTag("DriverListFragment");
fragment.setTargetFragment(ft, ResultCodeConstants.DRIVER_UPDATE_SUCCESS);
Bundle bundle = new Bundle();
// bundle.putSerializable("mySelectedDriver", selectedDriver2);
bundle.putParcelable("mySelectedDriver", selectedDriver2);
fragment.setArguments(bundle);
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frameLHaupt, fragment, "DriverUpdateFragment");
transaction.addToBackStack(null);
transaction.commit();
}
在 I just assign a random driverName 中,只是为了检查该值是否会在 ArrayList 中自动更改:DriverUpdateFragment
allDrivers
//Driver selectedDrivertest = (Driver) getArguments().getSerializable("mySelectedDriver");
Driver selectedDrivertest = (Driver) getArguments().getParcelable("mySelectedDriver");
selectedDrivertest.setDriverName("hey i am hardcoded updated");
Log.e("why is the value changed already in the main fragment???",""+ DriverListFragment.allDrivers); //here the value changes already. shouldn't the object be passed by value??????
我的 POJO 类如下所示:
@Entity(tableName = "Driver",indices = {@Index(value = {"driverName"},unique = true)})
public class Driver implements Serializable, Parcelable {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
@NotNull
private int id;
@ColumnInfo(name = "firstName")
@NotNull
private String firstName;
@ColumnInfo(name = "lastName")
@NotNull
private String lastName;
@ColumnInfo(name = "driverName")
@NotNull
private String driverName;
public Driver(@NotNull String firstName, @NotNull String lastName, @NotNull String driverName) {
this.firstName = firstName;
this.lastName = lastName;
this.driverName = driverName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NotNull
public String getFirstName() {
return firstName;
}
public void setFirstName(@NotNull String firstName) {
this.firstName = firstName;
}
@NotNull
public String getLastName() {
return lastName;
}
public void setLastName(@NotNull String lastName) {
this.lastName = lastName;
}
@NotNull
public String getDriverName() {
return driverName;
}
public void setDriverName(@NotNull String driverName) {
this.driverName = driverName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Driver driver = (Driver) o;
return getId() == driver.getId();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.firstName);
dest.writeString(this.lastName);
dest.writeString(this.driverName);
}
public void readFromParcel(Parcel source) {
this.id = source.readInt();
this.firstName = source.readString();
this.lastName = source.readString();
this.driverName = source.readString();
}
protected Driver(Parcel in) {
this.id = in.readInt();
this.firstName = in.readString();
this.lastName = in.readString();
this.driverName = in.readString();
}
public static final Parcelable.Creator<Driver> CREATOR = new Parcelable.Creator<Driver>() {
@Override
public Driver createFromParcel(Parcel source) {
return new Driver(source);
}
@Override
public Driver[] newArray(int size) {
return new Driver[size];
}
};
}
问题是 arraylist 已经更新了,即使是艰难的 Java 对象也总是根据文档按值传递?
我错过了什么?
从什么时候开始在 Java 中通过引用传递值?
我尝试使用可 parceable 和 serializable。
答: 暂无答案
上一个:切片是否按值传递?
评论
ArrayList
ArrayList
ArrayList
ArrayList