您好,登錄后才能下訂單哦!
http://blog.csdn.net/sukyle/article/details/4485505
1.Activity之間傳遞數據
(1)利用Intent傳遞數據
傳遞數據的Activity中:
Intent intent = new Intent();
intent.putExtra("name","Jon");//在Intent中加入鍵值對數據。鍵:name,值:Jon
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);
在取出數據的Activity中:
Intent intent = getIntent();//獲得傳過來的Intent。
String value = intent.getStringExtra("name");//根據鍵name取出值。
(2)利用Bundle傳遞數據
傳遞數據的Activity:
Intent intent = new Intent();
Bundle myBundle = new Bundle();
myBundle.putString("Key_Name","Tom");
intent.putExtras(myBundle);
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);
取出數據的Activity:
Bundle getBundle = getIntent().getExtras();
String value = getBundle.getString("Key_Name");
(3)利用startActivityForResult傳遞數據
startActivityForResult可以把數據傳過去,還可以把那邊的數據傳過來。
傳遞數據的Activity中:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("data", "somedata");//把數據傳過去
intent.putExtras(bundle);
intent.setClass(Activity01.this, Activity02.class);
startActivityForResult(intent, 10);//10是一個代碼
重載onActivityResult方法,用來接收傳過來的數據:
protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
switch (resultCode) {
case RESULT_OK:
Bundle b = intent.getExtras();
String str = b.getString("Result");
setTitle("Return data:" + str);
break;
default:
break;
}
}
接收數據的Activity:
Intent intent = getIntent();
Bundle getBundle = getIntent().getExtras();
String data = getBundle.getString("data");//讀取傳過來的數據
et.setText(data);
EditText edittext = (EditText) findViewById(R.id.text);
Intent intent = new Intent();//實例化一個Intent用來傳過去,可以在Intent里存放數據。
Bundle bundle = new Bundle();
bundle.putString("Result",edittext.getText().toString());
intent.putExtras(bundle);
Activity02.this.setResult(RESULT_OK,intent);//把Intent(數據)傳過去,RESULT_OK是請求碼。
finish();//結束當前的Activity。
2.http://xqjay19910131-yahoo-cn.iteye.com/blog/1280857
Activity之間傳遞對象,或者通過Bundle傳遞對象的兩種方式。
1:Serializable方式
傳遞一個對象
2:Parcelable方式
傳遞一個對象、傳遞多個對象(ArrayList<Object>)
方式一:Serializable
傳遞類:
public class CustomeClass implements Serializable{ /** * */ private static final long serialVersionUID = -7060210544600464481L; private String name; private String id; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
發送部分:
CustomeClass cc = new CustomeClass(); cc.setAge(21); cc.setId("123456"); cc.setName("mingkg21"); cc.setSex("男"); Intent intent = new Intent(this, PersonInfo.class); intent.putExtra("PERSON_INFO", cc); startActivity(intent);
接收部分:
Intent intent = getIntent(); CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO"); setTextView(R.id.id, cc.getId()); setTextView(R.id.name, cc.getName()); setTextView(R.id.sex, cc.getSex()); setTextView(R.id.age, String.valueOf(cc.getAge()));
方式二:Parcelable
傳遞類:
public class CustomeParcelable implements Parcelable { private String name; private String id; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Creator<CustomeParcelable>(){ public CustomeParcelable createFromParcel(Parcel source) { // TODO Auto-generated method stub CustomeParcelable cus = new CustomeParcelable(); cus.name = source.readString(); cus.id = source.readString(); cus.age = source.readInt(); cus.sex = source.readString(); return cus; } public CustomeParcelable[] newArray(int size) { // TODO Auto-generated method stub return new CustomeParcelable[size]; } }; public int describeContents() { // TODO Auto-generated method stub return 0; } public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(name); dest.writeString(id); dest.writeInt(age); dest.writeString(sex); } }
發送部分:
CustomeParcelable cc = new CustomeParcelable(); cc.setAge(21); cc.setId("123456"); cc.setName("mingkg21"); cc.setSex("男"); Intent intent = new Intent(this, PersonInfo.class); intent.putExtra("PERSON_INFO", cc); startActivity(intent);
接受部分:
Intent intent = getIntent(); CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO"); setTextView(R.id.id, cc.getId()); setTextView(R.id.name, cc.getName()); setTextView(R.id.sex, cc.getSex()); setTextView(R.id.age, String.valueOf(cc.getAge()));
以上為Parcelable傳遞一個對象,若要實現傳遞多個對象,
傳遞部分:
Bundle bundle = new Bundle(); bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs); msg.setData(bundle); endDocNotice.sendMessage(msg);
接受部分:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。