AndroidのIntent複雑データの伝達一(Objectタイプのデータ)

3353 ワード

Serializable方式の使用
前提:ObjectはSerializableインタフェースを実現する必要がある
Serializable方式伝達Objectの文法:bundle.putSerializable(key,object);
Serializable方式でObjectを受信構文:object=(Object)getIntent().getSerializableExtra(key); Serializableインタフェースを実現するには、オブジェクトをシーケンス化してから転送することであり、Javaの一般的なプログラミングとは明らかな違いはありません.また、Objectは明らかな変更を必要としません.この方法を推奨します.具体的な手順は以下の通りです.
1)PersonInfoエンティティークラス実装Serializableインタフェースを最初に作成し、name、address、ageプロパティを定義し、対応するget、setメソッドを生成します.具体的なコードは以下の通りです.
package zjh.android.bean;
 
import java.io.Serializable;
 
@SuppressWarnings("serial")
public class PersonInfo implements Serializable{
private String name;
private String address;
private int age;
public PersonInfo() {
}
 
public PersonInfo(String name,String address,int age){
this.name = name;
this.address = address;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

2)Serializableデータ型を送信するためのSendActivityクラスを作成します.具体的な実装コードは以下の通りです.
package zjh.android.lx;
 
import zjh.android.bean.PersonInfo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class SendActivity extends Activity {
private PersonInfo personInfo;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_send);
this.send = (Button)super.findViewById(R.id.send);
this.send.setOnClickListener(new OnClickListenerImpl());
}
private final class OnClickListenerImpl implements OnClickListener{
 
@Override
public void onClick(View v) {
personInfo = new PersonInfo("  ", "  ", 22);
Intent intent = new Intent(SendActivity.this,ReceiveActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("personInfo", personInfo);
intent.putExtras(bundle);
SendActivity.this.startActivity(intent);
}
}
 
}

3)Serializableデータ型を受信するReceiveActiveActivityクラスを作成します.コードは以下の通りです.
package zjh.android.lx;
 
import zjh.android.bean.PersonInfo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class ReceiveActivity extends Activity {
private TextView show;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.receive);
this.show = (TextView) super.findViewById(R.id.show);
Intent intent = super.getIntent();
PersonInfo personInfo = (PersonInfo) intent
.getSerializableExtra("personInfo");
this.show
.setText("name=" + personInfo.getName() + "
" + "address=" + personInfo.getAddress() + "
" + "age=" + personInfo.getAge()); } }

4)Android Mainfest.xmlファイルに対応するactivityを追加します.具体的には、次のとおりです.
<activity android:name="zjh.android.lx.ReceiveActivity"/>

このとき,Serializableデータ型のデータをIntentで伝達することで実現する.