0

I have two activities, MainActivityand MovieDetail and a java class MovieInfo.

MovieInfocorrectly initializes a static ArrayList< MovieInfo > with some MovieInfo instances.

MainActivity chooses one of the instantiated objects from the static ArrayList stored in MovieInfo and then passes it to MovieDetail activity via the putExtra method.

When I try to use the contains method in the static ArrayList from MainActivity with the instantiated object previously chosen it's working.

Why when I try the same from MovieDetail with the object received from MainActivity via the putExtra method it's doesnot recognize it anymore?

MainActivity

public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
static MovieInfo movie = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MovieInfo.setPtr(ptr);

    db = ptr.getReadableDatabase();
    ptr.onCreate(db);

    if (this.movie == null) this.movie = new MovieInfo(db);

    listView = (ListView) findViewById(R.id.movieList);

    adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, MovieInfo.getArrayMovieInfo());

    . . .

    /* When the user clicks on an item via the adapater */
    MovieInfo  movieSelected    = (MovieInfo) listView.getItemAtPosition(position);

    /* Test 1 */
    boolean test = MovieInfo.getArrayMovieInfo().contains(movieSelected);
    Log.d("1. Object recognise -> ", test ? "True" : "False");
    Log.d("1. Object Content", movieSelected.toString());

    Intent movieDetail = new Intent();
    movieDetail.setClass(getBaseContext(), MovieDetail.class);
    movieDetail.putExtra("Movie", movieSelected);
    startActivity(movieDetail);
}

MovieDetail

public class MovieDetail extends AppCompatActivity {

MovieInfo movie = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.movie_detail);

    movie = (MovieInfo) getIntent().getSerializableExtra("Movie");
    boolean test = MovieInfo.getArrayMovieInfo().contains(movie);

    /* Test 2 */
    Log.d("2. Object recognise -> ", test ? "True" : "False");
    Log.d("2. Object Content", movie.toString());

    /* The array is populated */
    for (MovieInfo curr_movie : MovieInfo.getArrayMovieInfo()) {
        Log.d("ARRAY MOVIE CONTENT : ", curr_movie.getTitle()+"\n");
    }

    . . .

}

MovieInfo

public class MovieInfo implements Serializable {
private static SQLiteDatabase db = null;
private static SQLiteOpenHelper ptr = null;

private static ArrayList<MovieInfo> arrayMovieInfo = new ArrayList<>();

/* Movie */
private int id;
private String title;
private String director;
private float rate;
private int c_id;

/* Cinema */
private String c_wording;
private String c_address;
private String c_city;
private String c_date;

/* Genre */
private String g_wording;

/* Actor */
private String a_name;

public MovieInfo(SQLiteDatabase db) {
    this.db         = db;
    MovieInfo movie;

    String[] fields = new String[] {"m_id, m_title, m_rate, c_id, m_director"};
    Cursor c = db.query("movie", fields, null, null, null, null, null);
    if (c.moveToFirst()) {
        //List all results
        do {
            movie = new MovieInfo(c.getInt(0), c.getString(1), c.getInt(2), c.getInt(3), c.getString(4));
            getArrayMovieInfo().add(movie);
        } while(c.moveToNext());
    }
}
}

Logcat output Test 1

08-18 14:00:45.230 15666-15666/com.example..movieticket D/1. Object recognise ->﹕ True 08-18 14:00:45.230 15666-15666/com.example..movieticket D/1. Object Content﹕ Ex Machina Your Rate : 3.0/5 2015-08-03 21:30:00 from Maxim at Helsinki

Logcat output Test 2

08-18 14:00:45.339 15666-15666/com.example..movieticket D/2. Object recognise ->﹕ False 08-18 14:00:45.339 15666-15666/com.example..movieticket D/2. Object Content﹕ Ex Machina Your Rate : 3.0/5 2015-08-03 21:30:00 from Maxim at Helsinki

Logcat output to check Arraylist content

/com.example..movieticket D/ARRAY MOVIE CONTENT :﹕ Ex Machina /com.example..movieticket D/ARRAY MOVIE CONTENT :﹕ Birdman /com.example.*.movieticket D/ARRAY MOVIE CONTENT :﹕ Titre

Ram
  • 3,092
  • 10
  • 40
  • 56
onepix
  • 289
  • 2
  • 11

2 Answers2

0

Okay, the point was to passed an object from an activity to another cause this object has already some data which will be use in the second activity. Even if the object received in the second activity has the same data compare to the one include in the ArrayList, they are not the same instance anymore because of the serialisation.

That's why I finally chose to passe only the id of the movie instead of serialise the object.

onepix
  • 289
  • 2
  • 11
0

You should use parcelable to send the list of object from one activity to another. See the example

Pass list of objects from one activity to other activity in android

Community
  • 1
  • 1
Vid
  • 1,012
  • 1
  • 14
  • 29
  • It's good to know this possibility but In my case I set the list with the static attribute to avoid passe the full list. It's wrong choice ? – onepix Aug 18 '15 at 14:16