0

I have a list with articles taken from an List and want to show the selected article in a PagerActivity so the user can easily flip to the article before and after when finished reading.

In iOS I can just pass the List (or a reference) with article objects to the PagerActivity. In Android however, calling on an Intent does not allow Lists to be passed on. So what would be the best way of doing it? Do I need to reload the array in the next Activity and then pass the position as an argument to the Intent?

(Reloading the List would be expensive, but should work if the DB hasn't changed since loading, otherwise the order might be different and the wrong item might be shown).

Luuk D. Jansen
  • 4,402
  • 8
  • 47
  • 90

2 Answers2

1

In Android

Used ParecleObjectif you have Custom Object ArrayList and if your have simple String ArrayList then pass directly in Intent

If you have Simple String ArrayList then refer below

Passing ArrayList of string arrays from one activity to another in android

and If you have Custom Object ArrayList then refer below

How to pass ArrayList<CustomeObject> from one activity to another?

Passing arraylist of objects between activities

Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
1

Considering your list of type Department here:

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

Now you simply have to put this list in Intent Bundle like this

bundle.putParcelableArrayList("Departments_KEY", departments);

and receive the list in your child activity like this

List<Department> departments = getIntent() or getArguments().getParcelable("Departments_KEY");
Danish Jamil
  • 1,090
  • 11
  • 31