how to clone an object in android?

前端 未结 6 1683
醉梦人生
醉梦人生 2021-01-07 18:43

What would be the best way to copy/clone an object in java/android?

rlBodyDataObj rlbo = bdoTable.get(name);

Right now the code assigns an

6条回答
  •  有刺的猬
    2021-01-07 19:23

    you can implements Parcelable (easy with studio plugin), and then

    public static  T copy(T orig) {
        Parcel p = Parcel.obtain();
        orig.writeToParcel(p, 0);
        p.setDataPosition(0);
        T copy = null;
        try {
            copy = (T) orig.getClass().getDeclaredConstructor(new Class[]{Parcel.class}).newInstance(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return copy;
    }
    

提交回复
热议问题