Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're in luck, all of your class' members are already serialzble so your first step is to say that <em>Lecture</em> is Serializable. </p> <pre><code>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; } </code></pre> <p>Next, you need to make a default constructor since serialization seems to require that. The last thing is you need to write your object out to a file. I usually use something like the following. Note this is for saving a game state so you might not want to use the cache directory. </p> <pre><code>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 */ } } } </code></pre> <p>Reading the data back is pretty symmetric to the write so I have left that out for this answer. Also, there are still a lot of caveats to Serialized objects so I suggest you do some Google searches and read up on Java serialization in general. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload