第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在android studio中使用parcelable在activity之間傳遞數(shù)據(jù)

如何在android studio中使用parcelable在activity之間傳遞數(shù)據(jù)

紫衣仙女 2024-01-28 16:26:47
我有一個(gè) recyclerview 顯示電影列表,我希望當(dāng)單擊的項(xiàng)目電影時(shí)可以使用 Parcelable 將數(shù)據(jù)傳遞給詳細(xì)信息這是我的 viewHolderAdapterpublic 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
查看完整描述

2 回答

?
天涯盡頭無(wú)女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

像這樣更改 itemviewclick


itemView.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);

                }

            });

并在細(xì)節(jié)上像這樣


添加這個(gè)


public static final String MOVIE = "movie";

在方法 onCreate() 中添加這個(gè)


YourList yourList = getIntent().getParcelableExtra(MOVIE);

之后,只需設(shè)置數(shù)據(jù)


textview.setText(yourList.getBlaBla());


查看完整回答
反對(duì) 回復(fù) 2024-01-28
?
揚(yáng)帆大魚(yú)

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

Intent支持三種傳遞數(shù)據(jù)的方式:


直接:將我們的數(shù)據(jù)直接放入意圖中


Bundle:創(chuàng)建一個(gè)bundle并在此處設(shè)置數(shù)據(jù)


Parcelable:這是一種“序列化”對(duì)象的方式。


傳遞數(shù)據(jù):直接


 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);

傳遞數(shù)據(jù):可打包


假設(shè)我們有一個(gè)名為 Person 的類,它包含三個(gè)屬性:姓名、姓氏和電子郵件。


現(xiàn)在,如果我們想傳遞這個(gè)類,它必須像這樣實(shí)現(xiàn)Parcelable接口


public class Person implements Parcelable {

private String name;

private String surname;

private String email;

// Get and Set methods


@Override

public int describeContents() {

return hashCode();

}


@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeString(name);

dest.writeString(surname);

dest.writeString(email);

}


// We reconstruct the object reading from the Parcel data

public Person(Parcel p) {

name = p.readString();

surname = p.readString();

email = p.readString();

}


public Person() {}


// We need to add a Creator

public static final Parcelable.Creator<person> CREATOR = new Parcelable.Creator<person>() {


@Override

public Person createFromParcel(Parcel parcel) {

return new Person(parcel);

}


@Override

public Person[] newArray(int size) {

return new Person[size];

}

};

現(xiàn)在我們只需這樣傳遞數(shù)據(jù):


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);

正如您所注意到的,我們只是將對(duì)象 Person 放入 Intent 中。當(dāng)我們收到數(shù)據(jù)時(shí),我們有:


Bundle b = i.getExtras();

Person p = (Person) b.getParcelable("myPers");

String name = p.getName();

String surname = p.getSurname();

String email = p.getEmail();


查看完整回答
反對(duì) 回復(fù) 2024-01-28
  • 2 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)