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();
添加回答
举报