国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現使用Intent傳輸包含自定義類的ArrayList示例

2019-10-22 18:30:38
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現使用Intent傳輸包含自定義類的ArrayList。分享給大家供大家參考,具體如下:

前言

之前項目中通過Intent只是傳輸簡單的字符串,這次因為需要在前一個頁面聯網獲取對象數據,然后在下一個頁面使用,所以考慮到使用Intent傳輸包含自定義類的ArrayList。

Serializable

Java的對象序列化指的是將那些實現了Serializable接口的對象轉換成一個字節序列,并且能在需要的時候再將這個字節序列完全恢復為之前的對象。

想實現對象的序列化,需要實現java.io.Serializable接口(注意,這個接口只是一個標記接口,并沒有具體需要override的方法)。當然,你也可以自己實現對象的序列化,但是我認為既然Java提供了這么一套對象序列化的機制,我們最好還是使用官方提供的方法。

Example

創建一個簡單對象,并且實現Serializable接口

package javastudy;import java.io.Serializable;public class Person implements Serializable {  private static final long serialVersionUID = -6470574927973900913L;  private String firstName;  private String secondName;  // example for transinet  private transient String noSerializableString;  public Person(String firstName, String secondName, String noSerializableString) {    super();    this.firstName = firstName;    this.secondName = secondName;    this.noSerializableString = noSerializableString;  }  public String getFirstName() {    return firstName;  }  public void setFirstName(String firstName) {    this.firstName = firstName;  }  public String getSecondName() {    return secondName;  }  public void setSecondName(String secondName) {    this.secondName = secondName;  }  public String getNoSerializableString() {    if (noSerializableString != null) {      return noSerializableString;    } else {      return "";    }  }  public void setNoSerializableString(String noSerializableString) {    this.noSerializableString = noSerializableString;  }  public String toString() {    return "Person [ first name :" + getFirstName() + ", second name :" + getSecondName() + ", no serializable :"        + getNoSerializableString() + "]";  }}

再寫一個類,用于實現對象的序列化和反序列化

package javastudy;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class TestSerializable {  public static void main(String[] args) {    Person person = new Person("Wang", "Zhengyi", "Genius");    String fileName = "/tmp/person.out";    // save object to file    FileOutputStream fos = null;    ObjectOutputStream out = null;    try {      fos = new FileOutputStream(fileName);      out = new ObjectOutputStream(fos);      out.writeObject(person);      out.flush();    } catch (Exception e) {      e.printStackTrace();    } finally {      if (fos != null) {        try {          fos.close();        } catch (IOException e) {          e.printStackTrace();        }      }      if (out != null) {        try {          out.close();        } catch (IOException e) {          e.printStackTrace();        }      }    }    // read object from file    FileInputStream fin = null;    ObjectInputStream in = null;    try {      fin = new FileInputStream(fileName);      in = new ObjectInputStream(fin);      Person p = (Person) in.readObject();      System.out.println(p);    } catch (Exception e) {      e.printStackTrace();    } finally {      if (fin != null) {        try {          fin.close();        } catch (IOException e) {          e.printStackTrace();        }      }      if (in != null) {        try {          in.close();        } catch (IOException e) {          e.printStackTrace();        }      }    }  }}

Intent傳輸包含自定義類的ArrayList

之所以之前介紹了Serializable,是因為這是實現Intent傳輸的前提,ArrayList包含的自定義類必須實現Serializable接口才能通過putSerializable()方法被傳遞。

還是用上面的Person類作為自定義的類,則第一個傳遞ArrayList的Activity關鍵代碼如下:

// Intent Creation and InitializationIntent passIntent = new Intent();passIntent.setClass(MainActivity.this, SecondaryActivity.class);// Create custom class ObjectPerson p1 = new Person("Wang", "Zhengyi", "first");Person p2 = new Person("Chen", "Shan", "second");// Create Array List of custom Class and add the Created objectArrayList<Person> aListClass = new ArrayList<Person>();aListClass.add(p1);aListClass.add(p2);// Create a Bundle and Put Bundle in to itBundle bundleObject = new Bundle();bundleObject.putSerializable("key", aListClass);// Put Bundle in to Intent and call start ActivitypassIntent.putExtras(bundleObject);startActivity(passIntent);

第二個接收ArrayList的Activity關鍵代碼如下:

try{  // Get the Bundle Object  Bundle bundleObject = getIntent().getExtras();    // Get ArrayList Bundle  ArrayList<Person> classObject = (ArrayList<Person>) bundleObject.getSerializable("key");    Retrieve Objects from Bundle  for(int index = 0; index < classObject.size(); index++){    Person person = classObject.get(index);    Toast.makeText(getApplicationContext(), person, Toast.LENGTH_SHORT).show();  }} catch(Exception e){  e.printStackTrace();}

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黎平县| 类乌齐县| 柏乡县| 略阳县| 荣成市| 梅州市| 台山市| 正宁县| 绥德县| 鹤山市| 湟中县| 盐城市| 澄城县| 延寿县| 皋兰县| 上饶县| 民乐县| 长乐市| 同仁县| 凯里市| 井研县| 雅江县| 广元市| 聂拉木县| 西青区| 武鸣县| 永泰县| 城步| 眉山市| 双城市| 郓城县| 密云县| 宾阳县| 纳雍县| 邢台县| 墨江| 察雅县| 且末县| 正蓝旗| 安西县| 广宁县|