将对象列表传递给 PagerActivity
Passing a list of objects to a PagerActivity
我有一个列表,其中包含取自列表的文章,我想在分页器中显示所选文章Activity,以便用户在完成阅读后可以轻松地翻到文章前后。
在 iOS 中,我可以将带有文章对象的列表(或引用)传递给寻呼机Activity。然而,在 Android 中,调用 Intent 不允许传递列表。那么最好的方法是什么?我是否需要在下一个 Activity 中重新加载数组,然后将位置作为参数传递给 Intent?
(重新加载列表的成本很高,但如果数据库在加载后没有发生变化,应该可以工作,否则顺序可能会不同,并且可能会显示错误的项目)。
在Android
Used ParecleObject
if you have Custom Object ArrayList
and if you have simple String ArrayList
then pass direct in Intent
如果你有简单的String ArrayList
然后参考下面
Passing ArrayList of string arrays from one activity to another in android
如果您有 Custom Object ArrayList
则参考下面
How to pass ArrayList<CustomeObject> from one activity to another?
Passing arraylist of objects between activities
考虑到您的部门类型列表:
public class Department implements Parcelable {
int department_id;
String title;
public Department() {
this.department_id = 0;
this.title = null;
}
public Department(int department_id, String title) {
this.department_id = department_id;
this.title = title;
}
public int getDepartmentId() {
return department_id;
}
public void setDepartmentId(int department_id) {
this.department_id = department_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flag) {
parcel.writeInt(department_id);
parcel.writeString(title);
}
public static final Creator<Department> CREATOR = new Creator<Department>(){
@Override
public Department createFromParcel(Parcel parcel) {
Department department = new Department();
department.setDepartmentId(parcel.readInt());
department.setTitle(parcel.readString());
return department;
}
@Override
public Department[] newArray(int size) {
return new Department[size];
}
};
}
List<Department> departments = new ArrayList<>();
现在您只需像这样将此列表放入 Intent Bundle
bundle.putParcelableArrayList("Departments_KEY", departments);
并在您的 child activity 中接收列表,就像这样
List<Department> departments = getIntent() or getArguments().getParcelable("Departments_KEY");
我有一个列表,其中包含取自列表的文章,我想在分页器中显示所选文章Activity,以便用户在完成阅读后可以轻松地翻到文章前后。
在 iOS 中,我可以将带有文章对象的列表(或引用)传递给寻呼机Activity。然而,在 Android 中,调用 Intent 不允许传递列表。那么最好的方法是什么?我是否需要在下一个 Activity 中重新加载数组,然后将位置作为参数传递给 Intent?
(重新加载列表的成本很高,但如果数据库在加载后没有发生变化,应该可以工作,否则顺序可能会不同,并且可能会显示错误的项目)。
在Android
Used ParecleObject
if you have Custom Object ArrayList
and if you have simple String ArrayList
then pass direct in Intent
如果你有简单的String ArrayList
然后参考下面
Passing ArrayList of string arrays from one activity to another in android
如果您有 Custom Object ArrayList
则参考下面
How to pass ArrayList<CustomeObject> from one activity to another?
Passing arraylist of objects between activities
考虑到您的部门类型列表:
public class Department implements Parcelable {
int department_id;
String title;
public Department() {
this.department_id = 0;
this.title = null;
}
public Department(int department_id, String title) {
this.department_id = department_id;
this.title = title;
}
public int getDepartmentId() {
return department_id;
}
public void setDepartmentId(int department_id) {
this.department_id = department_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flag) {
parcel.writeInt(department_id);
parcel.writeString(title);
}
public static final Creator<Department> CREATOR = new Creator<Department>(){
@Override
public Department createFromParcel(Parcel parcel) {
Department department = new Department();
department.setDepartmentId(parcel.readInt());
department.setTitle(parcel.readString());
return department;
}
@Override
public Department[] newArray(int size) {
return new Department[size];
}
};
}
List<Department> departments = new ArrayList<>();
现在您只需像这样将此列表放入 Intent Bundle
bundle.putParcelableArrayList("Departments_KEY", departments);
并在您的 child activity 中接收列表,就像这样
List<Department> departments = getIntent() or getArguments().getParcelable("Departments_KEY");