0

I'm new to coding. I am trying to pass the movie array and the date to the next Main_Activity3 so that it can be printed on the page. But i don't know how to put the data required into intent. For the movies, i would only need the selected movie to be passed forward to the other page.

Main_Activity2

private Spinner moviespinner,timespinner;
private ArrayList<movies> mvlist = new ArrayList<>();

private ImageView calandericon,arrowdownimage,arrowupimage,poster;
private EditText edittextDate;
private TextView ticketnumber,ratingsinfo,durationinfo,maincastinfo;
private Button butbook;
private int mYear, mMonth, mDay;
private int num = 0;
private String chosengenre;

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

    initUI();
    chosengenre = getIntent().getStringExtra("choice1");
    MovieList();

    ArrayAdapter<movies> movieAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, mvlist);
    moviespinner.setAdapter(movieAdapter);

    moviespinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            movies selected = mvlist.get(moviespinner.getSelectedItemPosition());

            poster.setImageResource(selected.getPoster());
            ratingsinfo.setText("Ratings: " + selected.getRating());
            durationinfo.setText(selected.getDuration());
            maincastinfo.setText("Main Casts: " + selected.getMain_cast());

            ArrayAdapter timeadapter = new ArrayAdapter<>(MainActivity2.this, android.R.layout.simple_spinner_dropdown_item, selected.getShow_time());
            timespinner.setAdapter(timeadapter);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    butbook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent booking = new Intent(MainActivity2.this, MainActivity3.class);
            booking.putParcelableArrayListExtra("movie", (ArrayList<? extends Parcelable>) mvlist);
            startActivity(booking);
        }
    });
}


private void initUI() {
    moviespinner= (Spinner) findViewById(R.id.moviespinner);
    calandericon=findViewById(R.id.calandericon);
    timespinner= (Spinner) findViewById(R.id.timespinner);
    arrowdownimage=findViewById(R.id.arrowdownimage);
    arrowupimage=findViewById(R.id.arrowupimage);
    edittextDate=findViewById(R.id.edittextDate);
    ratingsinfo=findViewById(R.id.ratingsinfo);
    durationinfo=findViewById(R.id.durationinfo);
    maincastinfo=findViewById(R.id.maincastinfo);
    ticketnumber=findViewById(R.id.ticketnumber);
    butbook=findViewById(R.id.butbook);

    calandericon.setOnClickListener(this);
    arrowdownimage.setOnClickListener(this);
    arrowupimage.setOnClickListener(this);
    butbook.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == calandericon){
        final Calendar calender = Calendar.getInstance();
        mYear = calender.get(Calendar.YEAR);
        mMonth = calender.get(Calendar.MONTH);
        mDay = calender.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog date = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

                edittextDate.setText(i2 + "-" + (i1 + 1) + "-" + i);
            }
        }, mYear, mMonth, mDay);
        date.show();
        date.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    }

    if (view == arrowupimage)
    {
        num += 1;
        ticketnumber.setText(Integer.toString(num));
    }

    if (view == arrowdownimage)
    {
        if (num > 0)
        {
            num -= 1;
        }
        ticketnumber.setText(Integer.toString(num));
    }
}

public void MovieList() {
    if (chosengenre.equals("SUPERHERO")){
        mvlist.add(new movies("Aquaman", "7.5","Jason Momoa","180 mins", new String[]{"11:30 am" , "12:00 pm", "1:40 pm", "5:30 pm", "7:30 pm"}, R.drawable.aquaman));
        mvlist.add(new movies("Avengers: Infinity War" , "9.0","Chris Hemsworth","150 mins", new String[]{"12:00 pm", "2:30 pm", "3:30 pm", "7:45 pm", "8:30 pm"}, R.drawable.avengeriw));
        mvlist.add(new movies("Dead Pool 2" , "8.3","Ryan Reynolds","120 mins", new String[]{"11:00 am", "1:30 pm", "7:00 pm", "8:30 pm"}, R.drawable.deadpool2));
        mvlist.add(new movies("Ant Man and the Wasp" , "8.5","Paul Rudd","120 mins", new String[]{"10:15 am", "12:20 pm", "3:30 pm", "7:15 pm", "9:30 pm"}, R.drawable.antman));
    }
    if (chosengenre.equals("ACTION")){
        mvlist.add(new movies("Mission Impossible: Fallout" , "7.2","Tom Cruise, Rebecca Ferguson","180 mins", new String[]{"10:30 am", "12:00 pm", "1:00 pm", "3:30 pm"}, R.drawable.mifallout));
        mvlist.add(new movies("Rampage" , "6.5","Dwayne Johnson","150 mins",new String[]{"9:30 am", "12:15 pm", "2:00 pm", "4:00 pm"}, R.drawable.rampage));
        mvlist.add(new movies("Jurassic World: Fallen Kingdom" , "7.5","Chris Pratt, Bryce Dallas","120 mins",new String[]{"11:40 am","1:30 pm","7:00 pm","9:00 pm"}, R.drawable.jwfallenkingdom));
        mvlist.add(new movies("Sky Scrapper" , "7.0","Dwayne Johnson, Neve Campbell","120 mins",new String[]{"9:30 am","11:00 pm","1:15 pm","6:30 pm","10:00 pm"}, R.drawable.skyscrapper));
        mvlist.add(new movies("The Meg" , "8.2","Jason Statham, Ruby Rose","120 mins",new String[]{"10:30 am", "11:00 pm", "1:30 pm", "7:00 pm", "9:00 pm"}, R.drawable.themeg));
    }
    if (chosengenre.equals("ANIMATION")){
        mvlist.add(new movies("Incredibles 2" , "7.2","NULL","90 mins",new String[]{"10:00 am", "1:45 pm", "3:30 pm"}, R.drawable.incredible2));
        mvlist.add(new movies("Hotel Transylvania 3" , "7.5","NULL","120 mins",new String[]{"12:00 pm", "1:00 pm", "3:30 pm", "4:30pm"}, R.drawable.hoteltrans3));
        mvlist.add(new movies("Peter Rabbit" , "6.4","NULL","120 mins",new String[]{"11:15 am", "1:30 pm", "3:00 pm", "7:30 pm"}, R.drawable.peterrabbit));
    }
}

Main_Activity3

private ImageView movieposter;
private TextView bookingtitle,bookingtimedate,bookingnumber;

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

    initUI();

    Intent booking = getIntent();
    final ArrayList<movies> mvlist = booking.getParcelableArrayListExtra("movie");
    
    int poster = mvlist.get
}

private void initUI() {
    movieposter = findViewById(R.id.movieposter);
    bookingtitle = findViewById(R.id.bookingtitle);
    bookingtimedate = findViewById(R.id.bookingtimedate);
    bookingnumber = findViewById(R.id.bookingnumber);
}

movies.java

public class movies implements Parcelable {

private String title;

private String rating;

private String main_cast;

private String duration;

private String[] show_time;

private int Poster;


public movies(String title, String rating, String main_cast, String duration, String[] show_time, int poster) {
    this.title = title;
    this.rating = rating;
    this.main_cast = main_cast;
    this.duration = duration;
    this.show_time = show_time;
    Poster = poster;
}

protected movies(Parcel in) {
    title = in.readString();
    rating = in.readString();
    main_cast = in.readString();
    duration = in.readString();
    show_time = in.createStringArray();
    Poster = in.readInt();
}

public static final Creator<movies> CREATOR = new Creator<movies>() {
    @Override
    public movies createFromParcel(Parcel in) {
        return new movies(in);
    }

    @Override
    public movies[] newArray(int size) {
        return new movies[size];
    }
};

public String getTitle() {
    return title;
}

public String getRating() {
    return rating;
}

public String getMain_cast() {
    return main_cast;
}

public String getDuration() {
    return duration;
}

public String[] getShow_time() {
    return show_time;
}

public int getPoster() {
    return Poster;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeString(rating);
    dest.writeString(main_cast);
    dest.writeString(duration);
    dest.writeStringArray(show_time);
    dest.writeInt(Poster);
}

@Override
public String toString() {
    return title;
}

}

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Removed `android-studio` tag as that tag is for problems/questions related to the Android Studio product. Your question is a generic Android question and has nothing to do with Android Studio. – David Wasser May 02 '21 at 16:09

1 Answers1

0

To send the selected movie and the date, you can do something like this: (Assuming that you don't need to send the entire list of movies, just the selected movie)

// set selectedDate = date in milliseconds as "long" variable

intent.putExtra("movie", selectedMovie);
intent.putExtra("date", selectedDate);

in the receiving Activity, get the data out of the Intent like this:

movies = intent.getParcelableExtra("movie");
dateInMillis = intent.getLongExtra("date");

If you have the date as mYear, mMmonth, mDay you can also add these separately, like this:

intent.putExtra("year", mYear);
intent.putExtra("month", mMonth);
intent.putExtra("day", mDay);

and extract them in the receiving Activity like this:

mYear = intent.getIntExtra("year", -1);
mMonth = intent.getIntExtra("month", -1);
mDay = intent.getIntExtra("day", -1);

NOTE: The -1 arguments are default values, in case the extras are not present in the Intent, which they should be.

David Wasser
  • 93,459
  • 16
  • 209
  • 274