2012-07-12 33 views
69

android.os.Message sử dụng Bundle để gửi bằng phương thức sendMessage. Do đó, có thể đặt HashMap bên trong một Bundle không?Android HashMap trong Gói?

+0

java và Android người mới 8) thnx cho tất cả! –

+0

Chấp nhận bất kỳ câu trả lời nào nếu chúng tôi giải quyết được sự cố của bạn. thx – AMerle

+3

Giải pháp là ở đây: [Android - Làm thế nào để vượt qua HashMap giữa các hoạt động?] (http://stackoverflow.com/a/11155142/379693) –

Trả lời

122

thử như:

Bundle extras = new Bundle(); 
extras.putSerializable("HashMap",hashMap); 
intent.putExtras(extras); 

và Hoạt động thứ hai

Bundle bundle = this.getIntent().getExtras(); 

if(bundle != null) { 
    hashMap = bundle.getSerializable("HashMap"); 
} 

Hashmap bởi dụng cụ mặc định Serializable để bạn có thể vượt qua nó bằng cách sử putSerializable trong Bundle và nhận được vào các hoạt động khác sử dụng getSerializable

+0

Tôi gặp lỗi khi cố gắng tuần tự hóa bản đồ băm liên kết Bưu kiện: không thể so sánh giá trị com .SGID.DataClass.InvalidSubscriptionReasonsData @ 41bd2b98 –

+0

Làm thế nào tôi có thể truy cập đối tượng serializable Hash_Map ??? –

+0

@Rani: vẫn gặp sự cố hoặc giải quyết? –

10

Theo số doc, Hashmap thực hiện Serializable, vì vậy bạn có thể putSerializable Tôi đoán vậy. Bạn đã thử à ?

2

Nếu bạn muốn gửi tất cả các phím trong gói, bạn có thể thử

for(String key: map.keySet()){ 
    bundle.putStringExtra(key, map.get(key)); 
} 
+0

Tôi tò mò nếu có lý do gì để không làm điều này? Tôi đã không sử dụng serialization nhiều, và không bao giờ bó (mới cho android), vì vậy tôi tò mò nếu điều này là O (n) và phụ thêm vào một bó có hiệu quả O (1) hay không. Nếu chúng không khác về chi phí, không thấy lý do tại sao bạn bận tâm với việc tuần tự hóa nó vào một bản đồ khác. –

4

Xin lưu ý: Nếu bạn đang sử dụng một AppCompatActivity, bạn sẽ phải gọi phương thức protected void onSaveInstanceState(Bundle outState) {} (KHÔNGpublic void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {}).

Ví dụ mã ...

Store bản đồ:

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putSerializable("leftMaxima", leftMaxima); 
    outState.putSerializable("rightMaxima", rightMaxima); 
} 

Và nhận được nó trong onCreate:

if (savedInstanceState != null) { 
    leftMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("leftMaxima"); 
    rightMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("rightMaxima"); 
} 

Xin lỗi nếu đó là một số loại một câu trả lời trùng lặp - có lẽ ai đó sẽ tìm thấy nó hữu ích. :)

0
public static Bundle mapToBundle(Map<String, Object> data) throws Exception { 
    Bundle bundle = new Bundle(); 
    for (Map.Entry<String, Object> entry : data.entrySet()) { 
     if (entry.getValue() instanceof String) 
      bundle.putString(entry.getKey(), (String) entry.getValue()); 
     else if (entry.getValue() instanceof Double) { 
      bundle.putDouble(entry.getKey(), ((Double) entry.getValue())); 
     } else if (entry.getValue() instanceof Integer) { 
      bundle.putInt(entry.getKey(), (Integer) entry.getValue()); 
     } else if (entry.getValue() instanceof Float) { 
      bundle.putFloat(entry.getKey(), ((Float) entry.getValue())); 
     } 
    } 
    return bundle; 
} 
0

Tôi đang sử dụng việc thực hiện kotlin của tôi để đạt được điều đó và cho đến nay nó hoạt động cho tôi. Hữu ích nếu bạn muốn tránh serializable nặng.

Cũng trong trật tự để làm việc tôi khuyên bạn nên sử dụng nó với these

Tuyên bố

class ParcelableMap<K,V>(val map: MutableMap<K,V>) : Parcelable { 
    constructor(parcel: Parcel) : this(parcel.readMap(LinkedHashMap<K,V>())) 

    override fun writeToParcel(parcel: Parcel, flags: Int) { 
     parcel.writeMap(map) 
    } 

    override fun describeContents(): Int { 
     return 0 
    } 

    companion object CREATOR : Parcelable.Creator<ParcelableMap<Any?,Any?>> { 
     @JvmStatic 
     override fun createFromParcel(parcel: Parcel): ParcelableMap<Any?,Any?> { 
      return ParcelableMap(parcel) 
     } 
     @JvmStatic 
     override fun newArray(size: Int): Array<ParcelableMap<Any?,Any?>?> { 
      return arrayOfNulls(size) 
     } 
    } 

} 

Sử dụng

ghi

val map = LinkedHashMap<Int, String>() 
val wrap = ParcelableMap<Int,String>(map) 
Bundle().putParcelable("your_key", wrap) 

đọc

val bundle = fragment.arguments ?: Bundle() 
val wrap = bundle.getParcelable<ParcelableMap<Int,String>>("your_key") 
val map = wrap.map 

Đừng quên rằng nếu bản đồ của bạn K,V không parceled theo mặc định thì phải thực hiện Parcelable