zoukankan      html  css  js  c++  java
  • android 之对象传递

    在android 的Activity 中,两个Activity 中对象的传递。

    通常我们使用意图来启动另一个Activity.如》 Intent intent = new Intent(MainActivity.this,OtherActivity.class);

    如果在两个Activity中传递数据就需要用到一个对象 Bundle bun = new Bundle();

    我们可以把Bundle 理解成一个容器,类似于map这样的东西。把"普通"对象放到里边,然后就可以在另一个Activity中获取该对象.

    比方说:

                  bun.putString("name","华仔");
    	      bun.putInt("age", 18);
    而在另一个Activity 中就可以用。    

       Intent intent = this.getIntent();
       String name = intent.getStringExtra("name");
    就可以获取到值。这里所说的普通对象。

    那么自定义对象在android中应该怎么实现呢。

    我记得在java语言中。如果对象网络上传输就得实现序列化。自定义类需要实现 java.io.Serializable.

    而在android 的应用中这样的方法同样可行代码如下。

    package com.hkrt.domain;
    /**person 实体*/
    public class Person implements java.io.Serializable {
    	
    	private static final long serialVersionUID = -1115440875848739771L;
    	private int id;
    	private String name;
    	private double money;
    
    	public Person(String name, double money) {
    		super();
    		this.name = name;
    		this.money = money;
    	}
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public double getMoney() {
    		return money;
    	}
    
    	public void setMoney(double money) {
    		this.money = money;
    	}
    
    	@Override
    	public String toString() {
    		return "Person [id=" + id + ", money=" + money + ", name=" + name + "]";
    	}
    
    }
    

    在android 中代码就可以用

    	 Person per = new Person("张三",5896.20);
    	 bun.putSerializable("person", per);
    进行数据传递。

    在另一个Activity 中就可以这样获取了。

    Bundle  bun =  intent.getExtras();
    Person per =	(Person)bun.getSerializable("person");

    第二种方法。就是自定义数据对象实现 android.os.Parcelable。译为:包裹

    同样可以理解成容器。代码如下:

    package com.hkrt.domain;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    public class Student implements android.os.Parcelable {
    	private String name;
    	private int age;
    	private double score;
    
    	public Student() {
    		super();
    	}
    	public Student(Parcel parcel) {
    		name = parcel.readString();
    		age = parcel.readInt();
    		score = parcel.readDouble();
    	}
    	
    	public Student(String name, int age, double score) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.score = score;
    	}
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public double getScore() {
    		 return score;
    	}
    
    	public void setScore(double score) {
    		this.score = score;
    	}
    
    	@Override
    	public int describeContents() {
    		return 0;
    	}
    
    	@Override
    	public void writeToParcel(Parcel parcel, int flags) {
    		parcel.writeString(name);
    		parcel.writeInt(age);
    		parcel.writeDouble(score);
    	}
    
    	 public static final Parcelable.Creator<Student> CREATOR  = new Parcelable.Creator<Student>() {
    		 public Student createFromParcel(Parcel in) {
    		     return new Student(in);
    		 }
    		
    		 public Student[] newArray(int size) {
    		     return new Student[size];
    		 }
    		};
    }
    
    官方解释如下:

    nterface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface.

    在android 的代码就可以这样写了。

       Student stu = new Student("李四", 20, 100.00);
       bun.putParcelable("stu", stu);
    而在另一个Activity中就可以获取到这个自定义对象Student.

    Student stu = (Student)bun.getParcelable("stu");

    结果图:





  • 相关阅读:
    win7开启Administrator账户
    二叉树遍历
    使用NAnt提高工作效率(二)
    系统服务的最简单实现
    右键附加启动命令行
    C#开发奇技淫巧二:根据dll文件加载C++或者Delphi插件
    百度原CTO李一男经典语录
    Sql开发技巧
    使用NAnt提高工作效率(一)
    对获取config文件的appSettings节点简单封装
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218334.html
Copyright © 2011-2022 走看看