Android - Passing an Object To Another Activity

后端 未结 3 1837
逝去的感伤
逝去的感伤 2021-01-27 06:49

I am utilizing the follow class, which I have as an object: http://pastebin.com/rKmtbDgF

And I am trying to pass it across using:

Intent booklist = new I         


        
3条回答
  •  伪装坚强ぢ
    2021-01-27 07:13

    Easiest Way to do this is to implement Serializeable ..

    import java.io.Serializable;
    
    @SuppressWarnings("serial") 
    public class Student implements Serializable {
    
    public Student(int age, String name){
        this.age = age;
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    private int age;
    private String name;
    
    }
    

    Sending object from Activity A to Activity B

    Student student = new Student (18,"Zar E Ahmer");
    Intent i = new Intent(this, B.class);
    i.putExtra("studentObject", student);
    startActivity(i);
    

    Getting Object in Activity B.

    Intent i = getIntent();
    Student student = (Student)i.getSerializableExtra("studentObject");
    

提交回复
热议问题