title: Persisting an ArrayList - cannot convert from Object to ArrayList
body: <p>Beating my head against the wall. I'm converting from Blackberry to Android.</p>
  
  <p>I have an app that needs to keep a log. It's a simple ArrayList and will not be that large. It does need to persist between app restarts etc. But it's not complex enough to bother with SQL. It needs to show in a ListView so an ArrayList seemed easiest.</p>
  
  <p>I seem to be able to serialize and store the ArrayList easily enough. But on reading it back in, it bombs saying I cannot cast from Object to ArrayList.</p>
  
  <p>My plan is to have the log object (singleton) load the history when I get the instance. Then I will have it write to a file with onDestroy()...</p>
  
  <p>On the Blackberry this kind of thing was easy. I'm surprised how much work you need to do on Android. Everything else on Android seems to be easier. I would prefer a way to flag an object as Persistent and have the system manage it for me. I know you can't easily stuff an Object into a SharedPreference either.</p>
  
  <p>Can someone please point out where I am going wrong? I've rewired it a couple of ways but the problem remains. If there is an easier way, please do tell.</p>
  
  <p>Thanks</p>
  
  <p>I have:</p>
  
  <pre><code>public class DataLog implements Serializable {
      private static final long serialVersionUID = 0L;
      private static String fileName = "datalog.dat";
  
      private static ArrayList&lt;String&gt; log = null;
  
      static public ArrayList&lt;String&gt; getInstance() {
          if (log == null) {
              synchronized (DataLog.class) {
                  if (log == null) {
                      log = new ArrayList&lt;String&gt;();
                  }
              }
              load();
          }
          return log;
      }
  
      public DataLog() {
          super();
      }
  
      public boolean add(String entry) {
          return log.add(entry);
      }
  
      public void add(int index, String entry) {
          if (index &gt; 0)
              log.add(index, entry);
          else
              log.add(entry);
          save();
      }
  
      public void clear() {
          log.clear();
      }
      public void save() {
          Persister.store(fileName, log);
      }
  
      public static void load() {
          log = (ArrayList&lt;String&gt;) Persister.restore(fileName);
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class Persister {
      static Context context = ThisApplication.context;
  
      public Persister() {}
  
      public static boolean store(String fileName, Object obj){
          File file = context.getFileStreamPath(fileName);
  
          FileOutputStream fos = null;
          ObjectOutputStream oos = null;
          try {
              fos = new FileOutputStream(file);
              oos = new ObjectOutputStream(fos);
              oos.writeObject(obj);
              oos.close();
              fos.close();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
          return true;
  
      }
      public static Object restore(String fileName){
          File file = context.getFileStreamPath(fileName);
          Object obj = new Object();
  
          if(file.exists() &amp;&amp; file.length()&gt;0){
              FileInputStream fis = null;
              ObjectInputStream ois = null;
              try {
                  fis = new FileInputStream(file);
                  ois = new ObjectInputStream(fis);
                  obj = ois.readObject();
                  ois.close();
                  fis.close();
              } catch (IOException ex) {
                  ex.printStackTrace();
              } catch (ClassNotFoundException ex) {
                  ex.printStackTrace();
              }
          }
          return obj;
      }
  }
  </code></pre>