Android编程实现使用Intent传输包含自定义类的ArrayList示例

 更新时间:2017年08月05日 12:24:29   作者:低调小一  
这篇文章主要介绍了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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android基础教程数据存储之文件存储

    Android基础教程数据存储之文件存储

    这篇文章主要介绍了Android基础教程数据存储之文件存储的相关资料,数据存储是Android开发的重要的知识,这里提供了实例,需要的朋友可以参考下
    2017-07-07
  • Android编程实现设置TabHost当中字体的方法

    Android编程实现设置TabHost当中字体的方法

    这篇文章主要介绍了Android编程实现设置TabHost当中字体的方法,涉及Android针对TabHost属性操作的相关技巧,非常简单实用,需要的朋友可以参考下
    2015-12-12
  • Android音视频开发Media FrameWork框架源码解析

    Android音视频开发Media FrameWork框架源码解析

    这篇文章主要为大家介绍了Android音视频开发Media FrameWork框架源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Android如何实现设备的异显功能详解

    Android如何实现设备的异显功能详解

    这篇文章主要给大家介绍了关于Android如何实现设备的异显功能的相关资料,这篇文章通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友可以参考下
    2022-02-02
  • Android 中SwipeRefreshLayout与ViewPager滑动事件冲突解决方法

    Android 中SwipeRefreshLayout与ViewPager滑动事件冲突解决方法

    这篇文章主要介绍了Android 中SwipeRefreshLayout与ViewPager滑动事件冲突解决方法的相关资料,需要的朋友可以参考下
    2017-04-04
  • RecyclerView实现流式标签单选多选功能

    RecyclerView实现流式标签单选多选功能

    RecyclerView是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足。这篇文章主要介绍了RecyclerView实现的流式标签单选多选功能,需要的朋友可以参考下
    2019-11-11
  • Android模拟实现网易新闻客户端

    Android模拟实现网易新闻客户端

    这篇文章主要为大家详细介绍了Android模拟实现网易新闻客户端,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 如何自己实现Android View Touch事件分发流程

    如何自己实现Android View Touch事件分发流程

    这篇文章主要介绍了如何自己实现Android View Touch事件分发流程,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-03-03
  • android之自定义Toast使用方法

    android之自定义Toast使用方法

    有时我们的程序使用默认的Toast时会和程序的整体风格不搭配,这个时候我们就需要自定义Toast,使其与我们的程序更加融合,使用自定义Toast,首先我们需要添加一个布局文件,该布局文件的结构和Activity使用的布局文件结构一致,在该布局文件中我们需设计我们Toast的布局
    2013-01-01
  • Flutter listview如何实现下拉刷新上拉加载更多功能

    Flutter listview如何实现下拉刷新上拉加载更多功能

    这篇文章主要给大家介绍了关于Flutter listview如何实现下拉刷新上拉加载更多功能的相关资料,对于新闻列表数据的更新和加载更多是必不可少的,而实现下拉刷新与上划加载更多的方式有很多种,需要的朋友可以参考下
    2021-08-08

最新评论