2 回答

TA貢獻1831條經驗 獲得超9個贊
像這樣更改 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);
}
});
并在細節上像這樣
添加這個
public static final String MOVIE = "movie";
在方法 onCreate() 中添加這個
YourList yourList = getIntent().getParcelableExtra(MOVIE);
之后,只需設置數據
textview.setText(yourList.getBlaBla());

TA貢獻1799條經驗 獲得超9個贊
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
@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];
}
};
現在我們只需這樣傳遞數據:
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();
添加回答
舉報