How to properly write Parcelable implementation for an Map?
up vote
0
down vote
favorite
I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .
I suppose for a Map< String , String > below is the correct implementation:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,String> entry : map.entrySet()){
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
String value = in.readString();
map.put(key,value);
}
}
But what about Map < String , Object > ?
add a comment |
up vote
0
down vote
favorite
I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .
I suppose for a Map< String , String > below is the correct implementation:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,String> entry : map.entrySet()){
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
String value = in.readString();
map.put(key,value);
}
}
But what about Map < String , Object > ?
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .
I suppose for a Map< String , String > below is the correct implementation:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,String> entry : map.entrySet()){
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
String value = in.readString();
map.put(key,value);
}
}
But what about Map < String , Object > ?
I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .
I suppose for a Map< String , String > below is the correct implementation:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,String> entry : map.entrySet()){
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
String value = in.readString();
map.put(key,value);
}
}
But what about Map < String , Object > ?
edited Nov 7 at 14:53
David Wasser
67.3k10136198
67.3k10136198
asked Nov 7 at 13:42
akshat tailang
234
234
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,Parcelable> entry : map.entrySet()){
out.writeString(entry.getKey());
entry.getValue().writeToParcel(out, flags);
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
Parcelable value = in.readParcelable(getClass().getClassLoader());
map.put(key,value);
}
}
HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:
public void writeToParcel(Parcel out, int flags){
out.writeValue(map);
}
private MyParcelable(Parcel in){
map = (Map)in.readValue(getClass().getClassLoader());
}
See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.
NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.
Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
– akshat tailang
Nov 9 at 16:26
What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
– David Wasser
Nov 9 at 17:13
Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
– David Wasser
Nov 10 at 18:47
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,Parcelable> entry : map.entrySet()){
out.writeString(entry.getKey());
entry.getValue().writeToParcel(out, flags);
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
Parcelable value = in.readParcelable(getClass().getClassLoader());
map.put(key,value);
}
}
HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:
public void writeToParcel(Parcel out, int flags){
out.writeValue(map);
}
private MyParcelable(Parcel in){
map = (Map)in.readValue(getClass().getClassLoader());
}
See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.
NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.
Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
– akshat tailang
Nov 9 at 16:26
What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
– David Wasser
Nov 9 at 17:13
Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
– David Wasser
Nov 10 at 18:47
add a comment |
up vote
1
down vote
You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,Parcelable> entry : map.entrySet()){
out.writeString(entry.getKey());
entry.getValue().writeToParcel(out, flags);
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
Parcelable value = in.readParcelable(getClass().getClassLoader());
map.put(key,value);
}
}
HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:
public void writeToParcel(Parcel out, int flags){
out.writeValue(map);
}
private MyParcelable(Parcel in){
map = (Map)in.readValue(getClass().getClassLoader());
}
See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.
NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.
Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
– akshat tailang
Nov 9 at 16:26
What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
– David Wasser
Nov 9 at 17:13
Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
– David Wasser
Nov 10 at 18:47
add a comment |
up vote
1
down vote
up vote
1
down vote
You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,Parcelable> entry : map.entrySet()){
out.writeString(entry.getKey());
entry.getValue().writeToParcel(out, flags);
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
Parcelable value = in.readParcelable(getClass().getClassLoader());
map.put(key,value);
}
}
HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:
public void writeToParcel(Parcel out, int flags){
out.writeValue(map);
}
private MyParcelable(Parcel in){
map = (Map)in.readValue(getClass().getClassLoader());
}
See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.
NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.
You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,Parcelable> entry : map.entrySet()){
out.writeString(entry.getKey());
entry.getValue().writeToParcel(out, flags);
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
Parcelable value = in.readParcelable(getClass().getClassLoader());
map.put(key,value);
}
}
HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:
public void writeToParcel(Parcel out, int flags){
out.writeValue(map);
}
private MyParcelable(Parcel in){
map = (Map)in.readValue(getClass().getClassLoader());
}
See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.
NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.
edited Nov 10 at 18:48
answered Nov 7 at 14:52
David Wasser
67.3k10136198
67.3k10136198
Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
– akshat tailang
Nov 9 at 16:26
What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
– David Wasser
Nov 9 at 17:13
Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
– David Wasser
Nov 10 at 18:47
add a comment |
Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
– akshat tailang
Nov 9 at 16:26
What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
– David Wasser
Nov 9 at 17:13
Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
– David Wasser
Nov 10 at 18:47
Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
– akshat tailang
Nov 9 at 16:26
Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
– akshat tailang
Nov 9 at 16:26
What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
– David Wasser
Nov 9 at 17:13
What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
– David Wasser
Nov 9 at 17:13
Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
– David Wasser
Nov 10 at 18:47
Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
– David Wasser
Nov 10 at 18:47
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190655%2fhow-to-properly-write-parcelable-implementation-for-an-mapstring-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown