Activity之間傳遞類對象

Activity之間傳遞類對象 


Activity之間通過Intent傳遞值,支持基本數據類型和String對象及它們的數組對象:
byte、byte[];char、char[];boolean、boolean[];short、short[];int、int[];long、long[];float、float[];double、double[];String、String[]。

其實,還有實現Serializable、Parcelable接口的類對象。


public class Person implements Serializable    
{    
    private static final long serialVersionUID = 7060210544600464481L;    
    private String name;    
    private String id;    
    private int age;    
    private String sex;    
                                   
    public String getName()    
    { return name; }    
                                   
    public void setName(String name)    
    { this.name = name; }    
                                   
    public String getId()     
    { return id; }    
                                   
    public void setId(String id)    
    { this.id = id; }    
                                   
    public int getAge()    
    { return age; }    
                                   
    public void setAge(int age)    
    {this.age = age; }    
                                   
    public String getSex()    
    { return sex; }    
                                   
    public void setSex(String sex)    
    { this.sex = sex; }    
}    
                                   
Person person = new Person();    
person.setAge(21);    
person.setId("123456");    
person.setName("mingkg21");    
person.setSex("男");    
Intent intent = new Intent(this, PersonBrowser.class);    
intent.putExtra("PERSON_INFO", person);    
startActivity(intent);    

  
Intent intent = getIntent();    
Person person = (Person)intent.getSerializableExtra("PERSON_INFO");    
setTextView(R.id.id, person.getId());    
setTextView(R.id.name, person.getName());    
setTextView(R.id.sex, person.getSex());    
setTextView(R.id.age, String.valueOf(person.getAge()));



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章