title: How to load serialized data into an ArrayList when no connection?
body: <p>Good Day!
  I'm my application has a spinner that drops down and on selection you get the title and author of that post in a list view. Which works fine. I am attempting to save by serialization also. Which I believe is working. </p>
  
  <blockquote>
    <p><strong>MY PROBLEM:</strong> <em>Once saved I want to load the data back into an ArrayList when there is no connection and display it in my ListView.</em></p>
  </blockquote>
  
  <p>I've seen several similar questions like the one below but still haven't been able to solve my problem. </p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/15684586/how-to-load-a-serialized-file-back-to-an-arraylist">How to load a serialized file back to an arrayList</a></p>
  </blockquote>
  
  <p>onCreate: Where I'm getting connection status</p>
  
  <pre><code>    // onCreate
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.i(TAG, "onCreate: RAN");
      Context context = this;
      final Spinner spinner = (Spinner) findViewById(R.id.querySpinner);
  
      listView = (ListView) findViewById(R.id.resultsListView);
      ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  
      if (manager != null) {
          NetworkInfo info = manager.getActiveNetworkInfo();
  
          if (info != null) {
              boolean isConnected = info.isConnected();
  
              // Network Operations
              if (isConnected) {
                  Log.i(TAG, "onCreate: CONNECTED TO A NETWORK");
  
                  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                      @Override
                      public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int pos, long id) {
                          DataTask task = new DataTask();
  
                          if (pos == 0) {
                              Log.i(TAG, "onItemSelected: Pandas");
                              task.execute("https://api.reddit.com/r/pandas");
                          } else if (pos == 1) {
                              Log.i(TAG, "onItemSelected: Koalas");
                              task.execute("https://api.reddit.com/r/koalas");
                          } else if (pos == 2) {
                              Log.i(TAG, "onItemSelected: Chimpanzees");
                              task.execute("https://api.reddit.com/r/chimpanzees");
                          } else if (pos == 3) {
                              Log.i(TAG, "onItemSelected: Emus");
                              task.execute("https://api.reddit.com/r/emus");
                          } else if (pos == 4) {
                              Log.i(TAG, "onItemSelected: Zebras");
                              task.execute("https://api.reddit.com/r/zebras");
                          }
                      }
  
                      @Override
                      public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {
                          Log.i(TAG, "onNothingSelected: NOTHING SELECTED");
                      }
                  });
  
              } else {
                  Log.i(TAG, "onCreate: NO CONNECTION");
                  Toast.makeText(context, R.string.toast_notConnected, Toast.LENGTH_LONG).show();
              }
          }
      }
  } // End onCreate
  </code></pre>
  
  <p>AsyncTask: Where I'm call saveSerialization()</p>
  
  <pre><code>// DataTask Class
  private class DataTask extends AsyncTask&lt;String, Void, String&gt;{
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          Log.i(TAG, "onPreExecute: RAN");
      }
  
      @Override
      protected String doInBackground(String... url) {
          Log.i(TAG, "doInBackground: RAN");
          return getNetworkData(url[0]);
      }
  
      @Override
      protected void onPostExecute(String s) {
          super.onPostExecute(s);
          Log.i(TAG, "onPostExecute: RAN");
  
          try {
              JSONObject outerObject = new JSONObject(s);
              JSONObject dataObject = outerObject.optJSONObject("data");
              JSONArray childrenArray = dataObject.optJSONArray("children");
  
              ArrayList&lt;Post&gt; postList = new ArrayList&lt;&gt;();
  
              for (int i = 0; i &lt;childrenArray.length() ; i++) {
                  Post post = new Post("","");
                  JSONObject data = childrenArray.optJSONObject(i).optJSONObject("data");
  
                  postTitle = MainActivity.this.getString(R.string.string_title) + " " + data.getString("title");
                  post.setmTitle(postTitle);
                  postAuthor = MainActivity.this.getString(R.string.string_author) + " " + data.getString("author");
                  post.setmAuthor(postAuthor);
  
                  postList.add(post);
                  saveSerializable(postList);
              }
              setupBaseAdapter(postList);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  } // End DataTask
  </code></pre>
  
  <p>saveSerialization() and loadSerialization() methods: Where I try to load back into an array list.</p>
  
  <pre><code> // saveSerializable
  private void saveSerializable(ArrayList&lt;Post&gt; arrposts) {
      Log.i(TAG, "saveSerializable: RAN");
  
      try {
          FileOutputStream fileOutputStream = openFileOutput("post.txt", MODE_PRIVATE);
          ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
          objectOutputStream.writeObject(arrposts);
          objectOutputStream.close();
      } catch(IOException e) {
          e.printStackTrace();
      }
  } // End saveSerializable
  
  private void loadSerializable() {
      Post post = null;
      try {
          FileInputStream fileInputStream = openFileInput("post.txt");
          ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
          post = (Post) objectInputStream.readObject();
          objectInputStream.close();
  
      } catch(IOException | ClassNotFoundException e) {
          e.printStackTrace();
      }
  
      if(post != null) {
  
      }
  
  }
  </code></pre>
  
  <p>Post Class: </p>
  
  <pre><code>public class Post implements Serializable {
  
  private String mTitle;
  private String mAuthor;
  
  public Post(String mTitle, String mAuthor) {
      this.mTitle = mTitle;
      this.mAuthor = mAuthor;
  }
  
  public String getmTitle() {
      return mTitle;
  }
  
  public void setmTitle(String mTitle) {
      this.mTitle = mTitle;
  }
  
  public String getmAuthor() {
      return mAuthor;
  }
  
  public void setmAuthor(String mAuthor) {
      this.mAuthor = mAuthor;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>PostAdapter: </p>
  
  <pre><code>class PostAdapter extends BaseAdapter {
  
  private final Random rand = new Random();
  private final int ID = rand.nextInt(Integer.MAX_VALUE);
  
  private final Context context;
  private List&lt;Post&gt; posts = new ArrayList&lt;&gt;();
  
  public PostAdapter(Context context, List&lt;Post&gt; posts) {
      this.context = context;
      this.posts = posts;
  }
  
  @Override
  public int getCount() {
      return (posts != null) ? posts.size() :0;
  }
  
  @Override
  public Post getItem(int pos) {
      return (posts != null &amp;&amp; pos &lt; posts.size() &amp;&amp; pos &gt;= 0) ? posts.get(pos) : null;
  }
  
  @Override
  public long getItemId(int pos) {
      return ID + pos;
  }
  
  @Override
  public View getView(int pos, View view, ViewGroup viewGroup) {
      ViewHolder holder;
  
      if (view == null) {
          view = LayoutInflater.from(context).inflate(R.layout.lv_redditposts, viewGroup, false);
          holder = new ViewHolder(view);
          view.setTag(holder);
      } else {
          holder = (ViewHolder) view.getTag();
      }
  
      Post post = getItem(pos);
      holder.postTitle.setText(post.getmTitle());
      holder.postAuthor.setText(post.getmAuthor());
  
      return view;
  }
  
  // ViewHolder
  static class ViewHolder {
      public final TextView postTitle;
      public final TextView postAuthor;
  
      public ViewHolder(View v){
          postTitle = (TextView) v.findViewById(R.id.postTitleTextView);
          postAuthor = (TextView) v.findViewById(R.id.postAuthorTextView);
      }
  } // End ViewHolder
  </code></pre>
  
  <p>}</p>