Android之旅十三 android中的數據傳遞方法

  android開發中,我們的Activity之間總避免不了進行數據的傳遞,幾種傳遞方式大致如下,各有各的用處:

1、Intent攜帶簡單的數據

Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("username","Mary");
bundle.putInt("age",23);
intent.putExtras(bundle);

 參數接收:

Bundle bundle=getIntent().getExtras(); 
String username=bundle.getString("username"); 
int age=bundle.getInt("age");


2、Intent攜帶例如ArrayList之類複雜的數據
 注意:在傳參數前,要用新增加一個List將對象包起來
 初始化數據:

Map<String,String> map=new HashMap<String,String>(); 
map.put("aaa","hello"); 
map.put("bbb","world"); 
List<Map<String,String>> list=new ArrayList<Map<String,String>>(); 
list.add(map); 
                                                                 
Intent intent=new Intent(); 
Bundle bundle=new Bundle(); 
//須定義一個list用於在bundle中傳遞需要傳遞的List<Object>,這個是必須要的 
ArrayList bundleList=new ArrayList(); 
bundleList.add(list); 
                                                                 
bundle.putParcelableArrayList("list",bundleList); 
intent.putExtras(bundle);

參數接收:

Bundle bundle=getIntent().getExtras(); 
List bundleList=bundle.getParcelbleArrayList("list"); 
List<Map<String,String>> list=(List<Map<String,String>>)bundleList.get(0); 
//遍歷輸出


3、通過實現Serializable接口傳遞參數
 初始化數據:

Map<String,String> map=new HashMap<String,String>(); 
map.put("aaa","hello"); 
map.put("bbb","world"); 
                                                             
Bundle bundle=new Bundle(); 
bundle.putSerializable("map",map); 
Intent intent=new Intent(); 
intent.putExtras(bundle);


參數接收:

Bundle bundle=getIntent().getExtras(); 
//HashMap沒問題 
//LinkedHashMap,轉換時會拋出ClassCastException,何解 
Map<String,String> map=(Map<String,String>)bundle.getSerializable("map"); 
//遍歷輸出 


4、通過實現Parcelable接口,把要傳的數據打包在裏面,然後在接收端自己分解出來。這個是Android獨有的,在其本身的源碼中也用得很多,效率要比Serializable相對要好。

Parcelable實現要點:需要實現三個東西

1)writeToParcel 方法。該方法將類的數據寫入外部提供的Parcel中.聲明如下:

writeToParcel (Parcel dest, int flags) 具體參數含義見javadoc

2)describeContents方法。沒搞懂有什麼用,反正直接返回0也可以

3)靜態的Parcelable.Creator接口,本接口有兩個方法:

createFromParcel(Parcel in) 實現從in中創建出類的實例的功能

newArray(int size) 創建一個類型爲T,長度爲size的數組,僅一句話(return new T[size])即可。估計本方法是供外部類反序列化本類數組使用。

定義一個DataParcelable實現Parcelable接口,將要傳輸的數據定義在這個類裏面:

package com.xin.activity; 
                                                       
import java.util.HashMap; 
import java.util.Map; 
                                                       
import android.os.Parcel; 
import android.os.Parcelable; 
                                                       
public class DataParcelable implements Parcelable{ 
    //定義要被傳輸的數據 
    public int age; 
    public String username; 
    public String password; 
    public Map<String,String> map=new HashMap<String,String>(); 
                                                       
    @Override 
    public int describeContents() { 
        return 0; 
    } 
                                                       
    /**
     * 類的數據寫入外部提供的Parcel
     */ 
    @Override 
    public void writeToParcel(Parcel out, int flags) { 
        //將數據映射到Parcel中去 
        out.writeInt(age); 
        out.writeString(username); 
        out.writeString(password); 
        out.writeMap(map); 
    } 
                                                           
    public static final Parcelable.Creator<DataParcelable> CREATOR=new Parcelable.Creator<DataParcelable>() { 
                                                       
        /**
         * 創建出類的實例
         */ 
        @SuppressWarnings("unchecked") 
        @Override 
        public DataParcelable createFromParcel(Parcel in) { 
            DataParcelable data=new DataParcelable(); 
            //將映射在Parcel對象中的數據還原出來 
            //這裏的順序一定要和writeToParcel中定義的順序一致才行 
            data.age=in.readInt(); 
            data.username=in.readString(); 
            data.password=in.readString(); 
            data.map=in.readHashMap(HashMap.class.getClassLoader()); 
            return data; 
        } 
                                                       
        @Override 
        public DataParcelable[] newArray(int size) { 
            return new DataParcelable[size]; 
        } 
    }; 
}

注意上面方法中createFromParcel讀取的順序要和writeToParcel寫入的順序一致才行,這樣才能保證相同數據類型的數據能夠按順序被讀取出來!裏面是這樣的一個設計機制.

初始化DataParcelable數據並將其放到Intent對象中去:

Intent intent=new Intent(MainActivity.this,OtherActivity.class); 
DataParcelable data=new DataParcelable(); 
data.age=23; 
data.username="hello"; 
data.password="world"; 
data.map=new HashMap<String,String>(); 
data.map.put("aaa","123"); 
data.map.put("bbb","456"); 
intent.putExtra("data", data); 
startActivity(intent);

讀取DataParcelable中的數據:

         Intent intent=getIntent(); 
DataParcelable data=intent.getParcelableExtra("data"); 
System.out.println("age="+data.age+",username="+data.username+",password="+data.password+",map="+data.map);
大家根據自己的需要和使用場景,選擇自己的數據存儲方式吧!!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章