疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Activity之间对象数据的传递


 

   

  传递对象用到intent,intent有这个方法putExtra(String name,Serializable)

  所以建立对象实体类实现Serializable

  public class Book implements Serializable{

  public String name;

  public double price;

  public Book() {

  super();

  }

  public Book(String name, double price) {

  super();

  this.name = name;

  this.price = price;

  }

  @Override

  public String toString() {

  return "Book [name=" + name + ", price=" + price + "]";

  }

  }

  在MainActivity中

  public class MainActivity extends Activity {

  private EditText et_name;

  private EditText et_price;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  et_name = (EditText) findViewById(R.id.et_name);

  et_price = (EditText) findViewById(R.id.et_price);

  }

  public void go(View v){

  String name = et_name.getText().toString();

  double price = Double.parseDouble(et_price.getText().toString());

  Intent intent = new Intent(this,OtherActivity.class);

  Book book = new Book(name, price);

  intent.putExtra("book", book);

  startActivity(intent);

  }

  }

  在OtherActivity中

  public class OtherActivity extends Activity {

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.other);

  //获取Intent

  Intent intent = getIntent();

  Book book = (Book) intent.getSerializableExtra("book");

  TextView tv_info = (TextView) findViewById(R.id.tv_info);

  tv_info.setText(book.toString());

  }

  }

  另外还有一种方式,intent还有这个方法,putExtra(String name,Parcelable value) 建立对象实体类实现Parcelable 这样方式在项目中用的更多

  使用原理是,把对象数据写到Parcel,然后从Parcel里读对象数据,就是这样一个过程

  public class Book implements Parcelable {

  public String name;

  public double price;

  public Book() {

  super();

  }

  public Book(String name, double price) {

  super();

  this.name = name;

  this.price = price;

  }

  @Override

  public String toString() {

  return "Book [name=" + name + ", price=" + price + "]";

  }

  public int describeContents() {

  return 0;

  }

  //注意:写入的顺序和读取的顺序必须一致

  //把数据写入到Parcel

  public void writeToParcel(Parcel dest, int flags) {

  dest.writeString(name);

  dest.writeDouble(price);

  }

  public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

  //创建对象 从Parcel中获取数据

  public Book createFromParcel(Parcel in) {

  Book book = new Book();

  book.name = in.readString();

  book.price = in.readDouble();

  return book;

  }

  public Book[] newArray(int size) {

  return new Book[size];

  }

  };

  }

  在MainActivity中

  public class MainActivity extends Activity {

  private EditText et_name;

  private EditText et_price;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  et_name = (EditText) findViewById(R.id.et_name);

  et_price = (EditText) findViewById(R.id.et_price);

  }

  public void go(View v){

  String name = et_name.getText().toString();

  double price = Double.parseDouble(et_price.getText().toString());

  Intent intent = new Intent(this,OtherActivity.class);

  intent.putExtra("book", new Book(name, price));

  startActivity(intent);

  }

  }

  在OtherActivity中

  public class OtherActivity extends Activity {

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.other);

  //获取Intent

  Intent intent = getIntent();

  Book book = intent.getParcelableExtra("book");

  TextView tv_info = (TextView) findViewById(R.id.tv_info);

  tv_info.setText(book.toString());

  }

  }

  /**

  * Seriailzable 和 Parcelable 的区别

  * Seriailzable 是java vm 识别的标示 重量级 性能低

  * Parcelable 是 Dalvik vm 识别的标示 轻量级 性能高 是支持 进程间传递参数

  *

  */