2 回答

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

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();
添加回答
舉報(bào)