Listviewitem 按引用传递,而不是按值传递(可解析和可序列化)

Listviewitem is passed by reference rather by value (parceable and serializable)

提问人:Nummer Eins 提问时间:5/29/2021 更新时间:5/29/2021 访问量:26

问:

我有一个简单的 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 中自动更改:DriverUpdateFragmentallDrivers

//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。

Java Android 引用 分包 按值传递

评论

0赞 Lebron11 5/29/2021
可能会检查这个“stackoverflow.com/questions/34770149/...”:)
1赞 David Wasser 5/30/2021
好吧,在 Java 中,对象总是通过引用传递的(从某种意义上说,对对象的引用是通过值传递的)。在 Java 中,只有基元是按值传递的。例如,如果你在 Java 中传递一个,被调用的方法不会收到 的副本,它会获得对 的引用,并且可以按照它想要的任何方式进行修改。ArrayListArrayListArrayListArrayList

答: 暂无答案