将捆绑包/意图传递给另一个活动?

Posted

技术标签:

【中文标题】将捆绑包/意图传递给另一个活动?【英文标题】:Passing bundles/intent to another activity? 【发布时间】:2021-07-16 21:39:22 【问题描述】:

我是编码新手。我正在尝试将电影数组和日期传递给下一个 Main_Activity3,以便它可以打印在页面上。但我不知道如何将所需的数据放入意图中。对于电影,我只需要将所选电影传递到其他页面。

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

公共类电影实现 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;

【问题讨论】:

删除了 android-studio 标签,因为该标签用于处理与 Android Studio 产品相关的问题/问题。您的问题是一个通用的 Android 问题,与 Android Studio 无关。 【参考方案1】:

要发送选定的电影和日期,您可以执行以下操作: (假设您不需要发送整个电影列表,只需发送选定的电影)

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

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

在接收Activity 时,像这样从Intent 中取出数据:

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

如果您的日期为mYear, mMmonth, mDay,您也可以单独添加这些日期,如下所示:

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

并像这样在接收Activity 中提取它们:

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

注意:-1 参数是默认值,以防 Intent 中不存在附加值,它们应该存在。

【讨论】:

以上是关于将捆绑包/意图传递给另一个活动?的主要内容,如果未能解决你的问题,请参考以下文章

使用意图将变量传递给另一个android活动

无法将 json 传递给另一个活动的意图

如何将收到的意图传递给另一个应用程序/活动?

如何将数组传递给另一个活动?

在方向更改后重新创建活动之前更改意图包数据

由意图传递或 android 中的活动更改引起的崩溃