Android中怎么使用Intent传输包含自定义类的ArrayList

发布时间:2021-07-22 16:48:39 作者:Leah
来源:亿速云 阅读:169

这篇文章给大家介绍Android中怎么使用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中怎么使用Intent传输包含自定义类的ArrayList就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Android中如何使用Intent
  2. android的Intent

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android intent arraylist

上一篇:Java中怎么实现一个求10到100000间的水仙花数算法

下一篇:Java中怎么实现一个数字时钟

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》