这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要介绍了 Android 编程实现使用 Intent 传输包含自定义类的 ArrayList, 涉及 Android 对象序列化、反序列化、Intent 数据传输等相关操作技巧, 需要的朋友可以参考下
本文实例讲述了 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 Initialization
- Intent passIntent = new Intent();
- passIntent.setClass(MainActivity.this, SecondaryActivity.class);
- // Create custom class Object
- Person p1 = new Person("Wang", "Zhengyi", "first");
- Person p2 = new Person("Chen", "Shan", "second");
- // Create Array List of custom Class and add the Created object
- ArrayList < Person > aListClass = new ArrayList < Person > ();
- aListClass.add(p1);
- aListClass.add(p2);
- // Create a Bundle and Put Bundle in to it
- Bundle bundleObject = new Bundle();
- bundleObject.putSerializable("key", aListClass);
- // Put Bundle in to Intent and call start Activity
- passIntent.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 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0823/344202.html