0

情况:我有一个安卓应用程序。在这个 android 应用程序中,我有一个包含“用户”类的内部文件(参见下面的代码),这个用户类有一个由支出组成的数组列表,“支出”类由几个不同的基本属性组成。

问题:当我从内部文件中获取此用户时,将“支出”对象添加到用户的数组列表中,然后重新保存此用户(删除文件并重新创建),它将支出数组列表的大小翻倍。我什至通过查看文件本身来观察它,并且可以清楚地看到每次添加支出时整个数组列表都会翻倍。用户似乎没有重复。我只尝试了相同的过程而没有增加支出,而且它保存得很好,没有重复。

用户类:

public class User implements Serializable {

    private int id_utilisateur;
    private String mail;
    private String motDePasse;
    private ArrayList<Spending> mySpendings;

    public Utilisateur(int id_utilisateur, String mail, String motDePasse) {
        this.id_utilisateur = id_utilisateur;
        this.mail = mail;
        this.motDePasse = motDePasse;
        this.mySpendings= new ArrayList<>();
    }

    //Getters and Setters of all attributes here//

    public void addSpending(Spending mySpending) {
        mySpendings.add(mySpending);
    }

}

我的消费类:

public class Spendingimplements Serializable {

private Integer idSpending;
private Date dateSpending;
private double montant;
private String pieceJoint;
private Magasin magasin;
private String domaine;
private Date garantieDebut;
private Date garantieFin;
private User user;

public Spending(Integer idSpending, Date dateSpending, double montant, User user, String domaine, Magasin magasin, String pieceJoint, Date garantieDebut, Date garantieFin) {
    this.idSpending= idSpending;
    this.dateSpending= dateSpending;
    this.montant = montant;
    this.user= user;
    this.domaine = domaine;
    this.magasin = magasin;
    this.pieceJoint = pieceJoint;
    this.garantieDebut = garantieDebut;
    this.garantieFin = garantieFin;
}

public Spending(Integer idSpending, Date dateSpending, double montant, User user, String domaine, Magasin magasin, String pieceJoint) {
    this.idSpending= idSpending;
    this.dateSpending= dateSpending;
    this.montant = montant;
    this.user= user;
    this.domaine = domaine;
    this.magasin = magasin;
    this.pieceJoint = pieceJoint;
    this.garantieDebut = null;
    this.garantieFin = null;
}

//geters and setters here//

}

我的班级 StorageHelper:

public final class StorageHelper {

    public StorageHelper() {}

    public static void storeObject(Context context, Object object) {

        try {
            File dir = context.getFilesDir();
            File file = new File(dir, "UserData.data");
            file.delete();

            FileOutputStream fos = context.openFileOutput("UserData.data", Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(object);
            os.close();
            fos.close();
        } catch (Exception e) {
            Log.e("userFile", "Error: Failed to save User into internal storage - \n" + e.toString());
        }

    }

    public static User getUser(Context context) {
        User mainUser = null;

        try {
            FileInputStream fis = context.openFileInput("UserData.data");
            ObjectInputStream is = new ObjectInputStream(fis);

            mainUser = (User) is.readObject();

            is.close();
            fis.close();

        } catch (Exception e) {
            Log.e("userFile", "Error: loading from the internal storage failed - \n" + e.toString());
        } finally {
            return mainUser;
        }

    }
}

主要活动 :

StorageHelper storageHelper;
User mainUser = storageHelper.getUser(this.getBaseContext());
mainUser.addSpending(mySpending);
storageHelper.storeObject(this.getBaseContext(), mainUser);
4

0 回答 0