如何存储自定义对象的arraylist?

b_y*_*yng 14 sqlite android

我创建了一个由自定义对象组成的arraylist.基本上用户将创建一个类,每次创建一个类时,都会在arraylist中添加一个新的Lecture(我的自定义对象).我需要保存生成的arraylist,以便即使重新启动应用程序也会保存用户的类.

根据我的理解,我必须使我的课程可序列化.但我究竟是怎么做到的呢?然后一旦它序列化我该怎么办?

public class Lecture{

public String title;
public String startTime;
public String endTime;
public String day;
public boolean classEnabled;

public Lecture(String title, String startTime, String endTime, String day, boolean enable){
    this.title = title;
    this.startTime = startTime;
    this.endTime = endTime;
    this.day = day;
    this.classEnabled = enable;
}
//Getters and setters below
Run Code Online (Sandbox Code Playgroud)

Squ*_*onk 14

我正在开发的Weather应用程序中使用一个类...

public class RegionList extends ArrayList<Region> {} // Region implements Serializeable
Run Code Online (Sandbox Code Playgroud)

为了保存,我使用这样的代码......

FileOutputStream outStream = new FileOutputStream(Weather.WeatherDir + "/RegionList.dat");
ObjectOutputStream objectOutStream = new ObjectOutputStream(outStream);
objectOutStream.writeInt(uk_weather_regions.size()); // Save size first
for(Region r:uk_weather_regions)
    objectOutStream.writeObject(r);
objectOutStream.close();
Run Code Online (Sandbox Code Playgroud)

注意:在编写Region对象之前,我编写一个int来保存列表的"大小".

当我回读时,我这样做......

FileInputStream inStream = new FileInputStream(f);
ObjectInputStream objectInStream = new ObjectInputStream(inStream);
int count = objectInStream.readInt(); // Get the number of regions
RegionList rl = new RegionList();
for (int c=0; c < count; c++)
    rl.add((Region) objectInStream.readObject());
objectInStream.close();
Run Code Online (Sandbox Code Playgroud)


And*_*ite 11

你很幸运,你班上的所有成员都已经串行了,所以你的第一步就是说Lecture是Serializable.

public class Lecture implements Serializable {

    public String title;
    public String startTime;
    public String endTime;
    public String day;
    public boolean classEnabled;

    public Lecture(String title, String startTime, String endTime, String day, boolean enable){
        this.title = title;
        this.startTime = startTime;
        this.endTime = endTime;
        this.day = day;
        this.classEnabled = enable;
    }
Run Code Online (Sandbox Code Playgroud)

接下来,您需要创建一个默认构造函数,因为序列化似乎需要这样做.最后一件事是您需要将对象写入文件.我通常使用类似下面的东西.请注意,这是为了保存游戏状态,因此您可能不想使用缓存目录.

private void saveState() {
    final File cache_dir = this.getCacheDir(); 
    final File suspend_f = new File(cache_dir.getAbsoluteFile() + File.separator + SUSPEND_FILE);

    FileOutputStream   fos  = null;
    ObjectOutputStream oos  = null;
    boolean            keep = true;

    try {
        fos = new FileOutputStream(suspend_f);
        oos = new ObjectOutputStream(fos);

        oos.writeObject(this.gameState);
    }
    catch (Exception e) {
        keep = false;
        Log.e("MyAppName", "failed to suspend", e);
    }
    finally {
        try {
            if (oos != null)   oos.close();
            if (fos != null)   fos.close();
            if (keep == false) suspend_f.delete();
        }
        catch (Exception e) { /* do nothing */ }
    }
}
Run Code Online (Sandbox Code Playgroud)

读回来的数据与写入非常对称,所以我把它留给了这个答案.此外,序列化对象仍然有很多警告,所以我建议你做一些谷歌搜索,并阅读一般的Java序列化.