Intent傳遞數據

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傳遞一個或者多個對象

Activity之間傳遞對象,或者通過Bundle傳遞對象的兩種方式。

1:Serializable方式
  傳遞一個對象

2:Parcelable方式
  傳遞一個對象、傳遞多個對象(ArrayList<Object>)


方式一:Serializable

     傳遞類:
    

Java代碼 複製代碼 收藏代碼
  1. public class CustomeClass implements Serializable{   
  2.        
  3.     /**  
  4.      *   
  5.      */  
  6.     private static final long serialVersionUID = -7060210544600464481L;   
  7.     private String name;   
  8.     private String id;   
  9.     private int age;   
  10.     private String sex;   
  11.        
  12.     public String getName() {   
  13.         return name;   
  14.     }   
  15.     public void setName(String name) {   
  16.         this.name = name;   
  17.     }   
  18.     public String getId() {   
  19.         return id;   
  20.     }   
  21.     public void setId(String id) {   
  22.         this.id = id;   
  23.     }   
  24.     public int getAge() {   
  25.         return age;   
  26.     }   
  27.     public void setAge(int age) {   
  28.         this.age = age;   
  29.     }   
  30.     public String getSex() {   
  31.         return sex;   
  32.     }   
  33.     public void setSex(String sex) {   
  34.         this.sex = sex;   
  35.     }   
  36.   
  37. }  




       發送部分:
      

Java代碼 複製代碼 收藏代碼
  1. CustomeClass cc = new CustomeClass();   
  2. cc.setAge(21);   
  3. cc.setId("123456");   
  4. cc.setName("mingkg21");   
  5. cc.setSex("男");   
  6.   
  7. Intent intent = new Intent(this, PersonInfo.class);   
  8. intent.putExtra("PERSON_INFO", cc);   
  9. startActivity(intent);  



       
       接收部分:
      

Java代碼 複製代碼 收藏代碼
  1.        Intent intent = getIntent();   
  2. CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");   
  3. setTextView(R.id.id, cc.getId());   
  4. setTextView(R.id.name, cc.getName());   
  5. setTextView(R.id.sex, cc.getSex());   
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));  




方式二:Parcelable

     傳遞類:
    

Java代碼 複製代碼 收藏代碼
  1. public class CustomeParcelable implements Parcelable {   
  2.        
  3.     private String name;   
  4.     private String id;   
  5.     private int age;   
  6.     private String sex;   
  7.   
  8.     public String getName() {   
  9.         return name;   
  10.     }   
  11.   
  12.     public void setName(String name) {   
  13.         this.name = name;   
  14.     }   
  15.   
  16.     public String getId() {   
  17.         return id;   
  18.     }   
  19.   
  20.     public void setId(String id) {   
  21.         this.id = id;   
  22.     }   
  23.   
  24.     public int getAge() {   
  25.         return age;   
  26.     }   
  27.   
  28.     public void setAge(int age) {   
  29.         this.age = age;   
  30.     }   
  31.   
  32.     public String getSex() {   
  33.         return sex;   
  34.     }   
  35.   
  36.     public void setSex(String sex) {   
  37.         this.sex = sex;   
  38.     }   
  39.        
  40.     public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Creator<CustomeParcelable>(){   
  41.   
  42.         public CustomeParcelable createFromParcel(Parcel source) {   
  43.             // TODO Auto-generated method stub   
  44.             CustomeParcelable cus = new CustomeParcelable();   
  45.             cus.name = source.readString();   
  46.             cus.id = source.readString();   
  47.             cus.age = source.readInt();   
  48.             cus.sex = source.readString();   
  49.             return cus;   
  50.         }   
  51.   
  52.         public CustomeParcelable[] newArray(int size) {   
  53.             // TODO Auto-generated method stub   
  54.             return new CustomeParcelable[size];   
  55.         }   
  56.            
  57.     };   
  58.   
  59.     public int describeContents() {   
  60.         // TODO Auto-generated method stub   
  61.         return 0;   
  62.     }   
  63.   
  64.     public void writeToParcel(Parcel dest, int flags) {   
  65.         // TODO Auto-generated method stub   
  66.         dest.writeString(name);   
  67.         dest.writeString(id);   
  68.         dest.writeInt(age);   
  69.         dest.writeString(sex);   
  70.     }   
  71.   
  72. }  



       發送部分:
      

Java代碼 複製代碼 收藏代碼
  1. CustomeParcelable cc = new CustomeParcelable();   
  2. cc.setAge(21);   
  3. cc.setId("123456");   
  4. cc.setName("mingkg21");   
  5. cc.setSex("男");   
  6.            
  7. Intent intent = new Intent(this, PersonInfo.class);   
  8. intent.putExtra("PERSON_INFO", cc);   
  9. startActivity(intent);  



        接受部分:
       

Java代碼 複製代碼 收藏代碼
  1. Intent intent = getIntent();   
  2. CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");   
  3. setTextView(R.id.id, cc.getId());   
  4. setTextView(R.id.name, cc.getName());   
  5. setTextView(R.id.sex, cc.getSex());   
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));  




以上爲Parcelable傳遞一個對象,若要實現傳遞多個對象,
傳遞部分:
 

Java代碼 複製代碼 收藏代碼
  1. Bundle bundle = new Bundle();   
  2.         bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);   
  3.         msg.setData(bundle);   
  4.         endDocNotice.sendMessage(msg);  



接受部分:
 

Java代碼  
  1. Bundle bundle =  msg.getData();   
  2.                 mP3TagForNetDTOs = bundle.getParcelableArrayList("mP3TagForNetDTOs");  



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章