How to include a List on a model?

Asked by At

So I currently have an employee model and I want to include something like a list of the employee's "position" or post. I looked for how to do this but there doesn't seem to a pretty way so that I could pass the Serializable model through a Intent Bundle.

This is my current model.

public class EmployeeDetails implements Serializable {
    int id, status, roleType;
    String firstName, lastName, passcode, email;
    int[] assignedTables;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getRoleType() {
        return roleType;
    }

    public void setRoleType(int roleType) {
        this.roleType = roleType;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPasscode() {
        return passcode;
    }

    public void setPasscode(String passcode) {
        this.passcode = passcode;
    }

    public int[] getAssignedTables() {
        return assignedTables;
    }

    public void setAssignedTables(int[] assignedTables) {
        this.assignedTables = assignedTables;
    }
}

The suggested post is a plain Arraylist and I already know how to use that. What I want is to pass a Serializable that contain an ArrayList. I wanted to change that int[] into a ArrayList.

3 Answers

0
rminaj On Best Solutions

This is what worked for me. https://stackoverflow.com/a/47053183/10464730

I created an ArrayList inside EmployeeDetails which would contain another model that implements Serializable as well.

0
Jitendra A On

ArrayList implements the Serializable interface. This means that if you want to change you int[] to ArrayList then you can directly pass your EmployeeDetails class through intents.

class A implements Serializable {
    ArrayList<Integer>a= new ArrayList<Integer>();
}

To send simply do :

 Bundle b = new Bundle();
 A a = new A();
 b.putSerializable("AA",a);
 Intent i = new Intent(Activity1.this, Activity2.class);
 i.putExtra("B",b);
 startActivity(i);
0
Animesh Roy On

Instead of using Serializable I recommend using Parcelable because Parcelable is faster than Serializable interface also Parcelable array can be passed via Intent in android.

Don't use: int[] assignedTables; use: private ArrayList<Integer> assignedTables;

Also, implement Parcelable and after implementing Parcelable android will show error just press the red bulb icon. press alt+Enter(windows) and this will implement the required method.

Check out this article about how you can implement parcelable