title: How can I populate a ListView on a SecondActivity with data from FirstActivity in Android?
body: <p>I'm working on a book-searching app project. The user enters a title, and the app searches Google Books for it.<br />
  I initially had one activity and layout. I decided to use two layouts (one for user to enter title, and the other displays results); had to create another activity because the results layout was throwing an exception about <em>'null' object reference</em>. <br />
  After creating the second activity, I used an intent to transfer List data between the two activities; no sooner did I find out that I had to use a <strong>Serialize</strong> or <strong>Parcelable</strong> object for the purpose.<br />
  Following instructions at <a href="https://stackoverflow.com/questions/12092612/pass-list-of-objects-from-one-activity-to-other-activity-in-android">Pass list of objects from one activity to other activity in android</a> &amp; <a href="https://www.vogella.com/tutorials/AndroidParcelable/article.html" rel="nofollow noreferrer">https://www.vogella.com/tutorials/AndroidParcelable/article.html</a>, I failed at implementing both, because <em>Serialize sent an empty ArrayList (thus, an empty results page, even though there're book hits)</em> and <em>Parcelable was throwing out different exceptions everytime I used it</em>, probably because I'm using an ArrayAdapter to populate the ListView with books.<br />
  I don't think I'm ready to implement the API at <a href="https://www.vogella.com/tutorials/AutoValue/article.html" rel="nofollow noreferrer">https://www.vogella.com/tutorials/AutoValue/article.html</a> and can't use Fragments as well. A better structure would be to use a single activity, <code>View.GONE</code> the background image I so much cherish, and display list of book objects under the search field. But it would be good to make a follow-up on Serialize &amp; Parcelable -- for future projects.
  So, fellow developers, what's the solution to this?</p>
  
  <p><a href="https://i.stack.imgur.com/Y7E3F.jpg" rel="nofollow noreferrer">FirstActivity screenshot</a>
  <a href="https://i.stack.imgur.com/WjFUi.jpg" rel="nofollow noreferrer">SecondActivity screeenshot</a></p>
  
  <p>Here's my code:
  MainActivity:</p>
  
  <pre><code>package project.android.gbookslisting;
  
  import android.app.LoaderManager;
  import android.content.Intent;
  import android.content.Loader;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import static project.android.gbookslisting.ResultsActivity.adapter;
  
  //import android.support.v4.content.AsyncTaskLoader;
  //import android.support.v7.app.AppCompatActivity;
  
  public class ParamsActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks&lt;List&lt;Book&gt;&gt;, Serializable {
  
      private static final int LOADER_ID = 0;
      private static String LOG_TAG = ParamsActivity.class.getName();
      EditText text;
      String query;
      private LoaderManager loaderManager = getLoaderManager();
  
      @Override
      protected void onCreate (Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.parameters);
  
  
          Button query = findViewById(R.id.search_button);
          text = findViewById(R.id.deets_field);
  
  
          query.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick (View view) {
                  if (text.getText().toString().length() &gt; 0) {                    loaderManager.initLoader(LOADER_ID, null, ParamsActivity.this);
                  } else if (text.getText().length() &lt; 1) {
                      text.setHint("Please enter book title/details");
                      text.setHintTextColor(Color.RED);
                  }
              }
          });
  
      }
  
      @Override
      public Loader&lt;List&lt;Book&gt;&gt; onCreateLoader (int i, Bundle bundle) {
          query = text.getText().toString();
          return new BookLoader(this, query);
      }
  
      @Override
      public void onLoadFinished (Loader&lt;List&lt;Book&gt;&gt; loader, List&lt;Book&gt; data) {
          // If there is a valid list of {@link Book}s, then add them to the adapter's dataset. This will trigger the ListView to update.
          if (data != null &amp;&amp; !data.isEmpty()) {
  
              data = new ArrayList&lt;Book&gt;();
  
              Intent i = new Intent(getApplicationContext(), ResultsActivity.class);
              i.putExtra("data", (Serializable) data);
              startActivity(i);
          }
      }
  
      @Override
      public void onLoaderReset (Loader loader) {
          adapter = new Adapt(this, new ArrayList&lt;Book&gt;());
          adapter.clear();
      }
  
  }
  </code></pre>
  
  <p>SecondActivity:</p>
  
  <pre><code>package project.android.gbookslisting;
  
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListView;
  import android.widget.TextView;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  public class ResultsActivity extends AppCompatActivity implements Serializable {
  
      static Adapt adapter;
      static TextView emptyResult;
      ListView bookEntries;
      String LOG_TAG = ResultsActivity.class.getName();
  
      @Override
      protected void onCreate (Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.hits_page);
  
          Intent i = getIntent();
          ArrayList&lt;Book&gt; books = (ArrayList&lt;Book&gt;) i.getSerializableExtra("data");
  
          emptyResult = findViewById(R.id.matches_nill);
          emptyResult.setText(R.string.matches0);
  
          if (!books.isEmpty()) {
              emptyResult.setVisibility(View.GONE);
  
              // Create a new adapter that takes a rich (or otherwise empty) list of books as input
              adapter = new Adapt(this, new ArrayList&lt;Book&gt;());
  
              // Get the list of books from {@link Search}
              bookEntries = findViewById(R.id.catalog);           
  
              bookEntries.setAdapter(adapter);
              bookEntries.setEmptyView(emptyResult);
  
              bookEntries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick (AdapterView&lt;?&gt; adapterView, View view, int position, long l) {
                      // Find the current book that was clicked on
                      Book currentBook = adapter.getItem(position);
  
                      // Convert the String URL into a URI object (to pass into the Intent constructor)
                      Uri bookUri = Uri.parse(currentBook.getPage());
  
                      // Create a new intent to view the book URI
                      Intent websiteIntent = new Intent(Intent.ACTION_VIEW, bookUri);
  
                      // Send the intent to launch a new activity
                      startActivity(websiteIntent);
                  }
              });
  
  //            adapter.clear();
  
              adapter.addAll(books);
          } else {
              emptyResult.setVisibility(View.VISIBLE);
              emptyResult.setText(R.string.matches0);
          }
  
      }
  }
  </code></pre>
  
  <p>Book obj.:</p>
  
  <pre><code>package project.android.gbookslisting;
  
  import java.io.Serializable;
  import java.util.Date;
  
  public class Book {
  
      private String book_title;
      private String author;
      private String publishing_year;
      private String page;
  
  
      public Book (String theTitle, String theAuthor, String theYear, String thePage) {
          this.book_title = theTitle;
          this.author = theAuthor;
          this.publishing_year = theYear;
          this.page = thePage;
      }
  
  
      public Book setBook_title (String book_title) {
          this.book_title = book_title;
          return this;
      }
  
      public Book setAuthor (String author) {
          this.author = author;
          return this;
      }
  
      public Book setPublishing_year (String publishing_year) {
          this.publishing_year = publishing_year;
          return this;
      }
  
      public Book setPage (String page) {
          this.page = page;
          return this;
      }
  
      protected String getAuthor () {
          return author;
      }
  
      protected String getPublishing_year () { return publishing_year; }
  
      protected String getPage () {
          return page;
      }
  
      protected String getBook_title () {
          return book_title;
      }
  }
  </code></pre>