我需要一种方法来保存ArrayList
对象。我已经浏览了该站点中的类似问题,并且我(似乎 ;-) 已经实现了我发现的内容,但是我遇到了两个问题:
- 如果我将类定义为
Serializable
并放置构造函数,它会在启动时崩溃 - 否则,它不保存数组
你能帮忙吗?我正在为一个志愿者的项目开发代码,但我被困住了......
非常感谢您。
我的应用程序具有以下定义的类:(Globals
文件 Globals.java)
public class Globals extends Application implements Serializable {
private int position=-1;
private ArrayList<RaccoltaPunti> raccoltePuntiList = new ArrayList<RaccoltaPunti>();
public static final long serialVersionUID = 1L;
/** constructor - seem required by Serializable, but creating it crashes app */
public Globals(int position, ArrayList<RaccoltaPunti> raccoltePuntiList) {
this.position = position;
this.raccoltePuntiList = raccoltePuntiList;
}
// {getters and setters…}
public void saveData(){
String filename = getResources().getString(R.string.GLB_filename);
String fileWithPath = this.getFilesDir().getPath().toString()+"/"+filename;
Toast.makeText(this, "Salvataggio testo..."+ fileWithPath, Toast.LENGTH_LONG).show();
try {
FileOutputStream fos = new FileOutputStream(fileWithPath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.raccoltePuntiList);
oos.close();
Toast.makeText(Globals.this, "DatiSalvati ", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("FileSave", "CDM - IOException", e);
Toast.makeText(this, "Errore saving file", Toast.LENGTH_LONG).show();
}
}
}
引用的类是:RaccoltaPunti.java
public class RaccoltaPunti {
private String nomeRaccolta;
private String nomePromoter;
private String numeroTessera;
private Long puntiPusseduti;
private String dataScadenzaPunti;
private String sitoWeb;
private String sitoWebUsername;
// constructor, getters and setters…….
}