0

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.

rminaj
  • 560
  • 6
  • 28

3 Answers3

0

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);
Jitendra A
  • 1,568
  • 10
  • 18
  • I'm already doing that for `EmployeeDetails` and I want to know how to have an `ArrayList` or any a `Hashmap` inside `EmployeeDetails` that would contain the position or post of the employee. – rminaj May 02 '19 at 04:21
  • As I mentioned its you don't have to do anything. The class A in my example above also has an Integer arraylist and the whole class A is serializable – Jitendra A May 02 '19 at 08:34
  • Ok. I'll try this when I got time. Thanks – rminaj May 03 '19 at 05:33
0

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

0

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.

rminaj
  • 560
  • 6
  • 28