Intent.putExtra()傳遞Object對象或者ArrayList<Object> (轉)

來源:互聯網
上載者:User

標籤:

Intent傳遞基本類型相信大家都十分熟悉,如何傳遞Object對象或者ArrayList<Object>對象呢?

可以通過:

(1)public Intent putExtra (String name, Serializable value)

(2)public Intent putExtra (String name, Parcelable value)

       public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

 

一、通過實現Serializable介面傳遞

(1)首先讓Person繼承Serializable介面

 

查看文本列印
  1. package com.example.hellojni;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Person implements Serializable{  
  6.   
  7.     /** 
  8.      * Auto generate 
  9.      */  
  10.     private static final long serialVersionUID = -5053412967314724078L;  
  11.   
  12.     private String name;  
  13.       
  14.     private int age;  
  15.       
  16.     public Person(String name, int age) {  
  17.         this.name = name;  
  18.         this.age = age;  
  19.     }  
  20.   
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.   
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     public int getAge() {  
  30.         return age;  
  31.     }  
  32.   
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.       
  37. }  
查看文本列印
  1. package com.example.hellojni;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Person implements Serializable{  
  6.   
  7.     /** 
  8.      * Auto generate 
  9.      */  
  10.     private static final long serialVersionUID = -5053412967314724078L;  
  11.   
  12.     private String name;  
  13.       
  14.     private int age;  
  15.       
  16.     public Person(String name, int age) {  
  17.         this.name = name;  
  18.         this.age = age;  
  19.     }  
  20.   
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.   
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     public int getAge() {  
  30.         return age;  
  31.     }  
  32.   
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.       
  37. }  

 

 

 

(2)通過Intent來進行傳輸,具體方法是:public Intent putExtra (String name, Serializable value)

 

查看文本列印
  1. Intent intent = new Intent(this, OtherActivity.class);  
  2. intent.putExtra("person", new Person("bear", 22));  
  3. startActivity(intent);  
查看文本列印
  1. Intent intent = new Intent(this, OtherActivity.class);  
  2. intent.putExtra("person", new Person("bear", 22));  
  3. startActivity(intent);  

 

 

(3)另外一個Activity中列印值:

 

查看文本列印
  1. Intent intent = getIntent();  
  2. Person person = (Person)intent.getSerializableExtra("person");  
  3. System.out.println("name:" + person.getName());  
  4. System.out.println("age:" + person.getAge());  
查看文本列印
  1. Intent intent = getIntent();  
  2. Person person = (Person)intent.getSerializableExtra("person");  
  3. System.out.println("name:" + person.getName());  
  4. System.out.println("age:" + person.getAge());  



(4)傳遞ArrayList<Person>:

 

查看文本列印
  1.    Intent intent = new Intent(this, OtherActivity.class);  
  2. ArrayList<Person> personList= new ArrayList<Person>();  
  3. initList(personList);  //初始化personList  
  4.    intent.putExtra("personList", personList);  
  5.    startActivity(intent);  
查看文本列印
  1.    Intent intent = new Intent(this, OtherActivity.class);  
  2. ArrayList<Person> personList= new ArrayList<Person>();  
  3. initList(personList);  //初始化personList  
  4.    intent.putExtra("personList", personList);  
  5.    startActivity(intent);  

 

 

(注意傳實值型別必須是ArrayList而不能是List。調用的還是public Intent putExtra (String name, Serializable value)這個函數,另外一邊擷取的時候強制轉化為ArrayList<Person>即可)

 

 

二、通過實現Parcelable介面傳遞

同上,Person類實現Parcelable介面即可:


傳遞Object用:public Intent putExtra (String name, Parcelable value)
傳遞ArrayList<Object>用:public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

(同是序列化的介面,為神馬Parcelable分開提供了2個函數,而傳Serializable只提供了一個?這裡我也很不解。。。)

 

轉自:連結

Intent.putExtra()傳遞Object對象或者ArrayList<Object> (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.