如何在android studio中使用parcelable在activity之间传递数据

我有一个 recyclerview 显示电影列表,我希望当单击的项目电影时可以使用 Parcelable 将数据传递给详细信息


这是我的 viewHolderAdapter


public class MovieVHolder extends RecyclerView.ViewHolder {


        TextView mTxtTitleMovie, mTxtDescriptionMovie, mTxtDateMovie;

        ImageView mImgPosterMovie;


        public MovieVHolder(@NonNull final View itemView) {

            super(itemView);

            mTxtTitleMovie = itemView.findViewById(R.id.txt_title_movie);

            mTxtDescriptionMovie = itemView.findViewById(R.id.txt_desc_movie);

            mTxtDateMovie = itemView.findViewById(R.id.txt_date_movie);

            mImgPosterMovie = itemView.findViewById(R.id.img_movie);


            itemView.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View view) {

                    Intent i = new Intent(context, DetailActivity.class);

                    context.startActivity(i);

                }

            });


        }


        public void bind(ListMovieEntity listMovieEntity) {

            mTxtTitleMovie.setText(listMovieEntity.getMovieTittle());

            mTxtDescriptionMovie.setText(listMovieEntity.getMovieDescription());

            mTxtDateMovie.setText(listMovieEntity.getMovieDate());

            Glide.with(context)

                    .load("https://image.tmdb.org/t/p/w185/"+listMovieEntity.getMoviePosterPath())

                    .into(mImgPosterMovie);

        }

    }

我在模型类中添加了 Parcelable


紫衣仙女
浏览 94回答 2
2回答

天涯尽头无女友

像这样更改 itemviewclickitemView.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                    Intent i = new Intent(context, DetailActivity.class);    //addthis       i.putExtra(DetailActivity.MOVIE, entListMovie.get(getPosition()));                    context.startActivity(i);                }            });并在细节上像这样添加这个public static final String MOVIE = "movie";在方法 onCreate() 中添加这个YourList yourList = getIntent().getParcelableExtra(MOVIE);之后,只需设置数据textview.setText(yourList.getBlaBla());

扬帆大鱼

Intent支持三种传递数据的方式:直接:将我们的数据直接放入意图中Bundle:创建一个bundle并在此处设置数据Parcelable:这是一种“序列化”对象的方式。传递数据:直接 Intent i = new Intent(context, DetailActivity.class);i.putExtra("title", mTxtTitleMovie.getText().toString();i.putExtra("surname", edtSurname.getText().toString();i.putExtra("email", edtEmail.getText().toString();context.startActivity(i);捆Intent i = new Intent(context, DetailActivity.class);Bundle b = new Bundle();b.putString("name", edtName.getText().toString());b.putString("surname", edtSurname.getText().toString());b.putString("email", edtEmail.getText().toString());i.putExtra("personBdl", b);context.startActivity(i);传递数据:可打包假设我们有一个名为 Person 的类,它包含三个属性:姓名、姓氏和电子邮件。现在,如果我们想传递这个类,它必须像这样实现Parcelable接口public class Person implements Parcelable {private String name;private String surname;private String email;// Get and Set methods@Overridepublic int describeContents() {return hashCode();}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeString(surname);dest.writeString(email);}// We reconstruct the object reading from the Parcel datapublic Person(Parcel p) {name = p.readString();surname = p.readString();email = p.readString();}public Person() {}// We need to add a Creatorpublic static final Parcelable.Creator<person> CREATOR = new Parcelable.Creator<person>() {@Overridepublic Person createFromParcel(Parcel parcel) {return new Person(parcel);}@Overridepublic Person[] newArray(int size) {return new Person[size];}};现在我们只需这样传递数据:Intent i = new Intent(EditActivity.this, ViewActivity.class);Person p = new Person();p.setName(edtName.getText().toString());p.setSurname(edtSurname.getText().toString());p.setEmail(edtEmail.getText().toString());i.putExtra("myPers", p);startActivity(i);正如您所注意到的,我们只是将对象 Person 放入 Intent 中。当我们收到数据时,我们有:Bundle b = i.getExtras();Person p = (Person) b.getParcelable("myPers");String name = p.getName();String surname = p.getSurname();String email = p.getEmail();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java