Activity之间的数据类传递 Parcelable接口和serializable系列,都是用来在不同界面之间进行类形式的数据交互。
1:Parcelable用法:几个界面之间的传递类数据
* 1:定义的数据类实现接口Parcelable* 2:重写数据类的 writeToParcel(Parcel arg0, int arg1)方法 用来写入数据
* 3:该数据类必须有一个静态领域 而且静态变量名字必须是CREATOR
* public static final Parcelable.Creator<Data> CREATOR 用来读出写入的数据
Demo:
package com.example.intent_class;
import android.os.Parcel;
import android.os.Parcelable;
/**
* intent的传递类 把该数据类拿来在界面1与界面2之间传递
* @author
*
*/
public class Data implements Parcelable{
private int id;
private String name;
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 Data(int id, String name)
{
this.id = id;
this.name = name;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/**
* 将数据写入Parcel
*/
@Override
public void writeToParcel(Parcel arg0, int arg1) {
// TODO Auto-generated method stub
//将数据写入Parcel里去
arg0.writeInt(id);
arg0.writeString(name);
}
//该静态领域是必须有的 而且静态变量名字必须是CREATOR
public static final Parcelable.Creator<Data> CREATOR=new Parcelable.Creator<Data>(){
@Override
public Data createFromParcel(Parcel arg0) {
// TODO Auto-generated method stub
//将数据从Parcel读出
int num=arg0.readInt();
String str=arg0.readString();
return new Data(num,str);
}
@Override
public Data[] newArray(int arg0) {
// TODO Auto-generated method stub
return null;
}
};
}
public class MainActivity extends Activity {
Intent intent;
Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data=new Data(11,"Data类数据1");
intent=new Intent(MainActivity.this,Activity2.class);
}
public void myclick(View v)
{
Bundle bundle=new Bundle();
bundle.putParcelable("bundle1", data);
intent.putExtra("intent1", bundle);
startActivity(intent);
}
package com.example.intent_class;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
/**
* 界面2
* @author LiTing
*
*/
public class Activity2 extends Activity {
Intent intent;
Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2);
//获取从界面1传递来的数据
intent=getIntent();
Bundle bundle=intent.getBundleExtra("intent1");
//得到传递来的数据类
data=bundle.getParcelable("bundle1");
Toast.makeText(this, data.getName(), 0).show();
}
public void myclick(View v)
{
//返回到界面1
intent=new Intent(Activity2.this,MainActivity.class);
startActivity(intent);
finish();
}
}
2可以向Bundle放入一个可序化对象 Serializable
serializable系列的用法:
* 1:数据类实现接口Serializable
* 2:数据类需要定义一个private static final long serialVersionUID = 1L;//版本序列号
* 3:完成
Demo2:
package com.example.intent_serializable_class;
import java.io.Serializable;
import android.os.Parcel;
import android.os.Parcelable;
/**
* intent的传递类 把该数据类拿来在界面1与界面2之间传递
* @author LiTing
*
*/
public class Data implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;//版本序列号
private int id;
private String name;
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 Data(int id, String name)
{
this.id = id;
this.name = name;
}
}
package com.example.intent_serializable_class;
import java.io.Serializable;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
/**
* serializable用法的几个界面之间的传递类数据
* 1:数据尅实现接口Serializable
* 2:数据类需要定义一个private static final long serialVersionUID = 1L;//版本序列号
* 3:完成
* @author
*
*/
public class MainActivity extends Activity {
Intent intent;
Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data=new Data(11,"Data类数据1");
intent=new Intent(MainActivity.this,Activity2.class);
}
public void myclick(View v)
{
Bundle bundle=new Bundle();
bundle.putSerializable("bundle1", data);
intent.putExtra("intent1", bundle);
startActivity(intent);
}
}
package com.example.intent_serializable_class;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
/**
* 界面2
* @author
*
*/
public class Activity2 extends Activity {
Intent intent;
Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2);
//获取从界面1传递来的数据
intent=getIntent();
Bundle bundle=intent.getBundleExtra("intent1");
//得到传递来的serializable数据类
data=(Data) bundle.getSerializable("bundle1");
Toast.makeText(this, data.getName(), 0).show();
}
public void myclick(View v)
{
//返回到界面1
intent=new Intent(Activity2.this,MainActivity.class);
startActivity(intent);
finish();
}
}