How to extract values from bundle in Android

saran picture saran · Jan 28, 2013 · Viewed 15.3k times · Source

While sending requests via Facebook_Android SDK, I get a bundle in return. Can someone explain what data type it is and how to extract the data in it? Thanks.

01-28 11:58:07.548: I/Values(16661): Bundle[{to[0]=100005099741441, to[1]=100005089509891, request=134129756751737}]

EDIT Here, to[i] is a string array. I was able to do it. but I don't think its the right way to do it.

for(int i=0;i< size-1;i++){
System.out.println(values.getString("to["+i+"]"));
}

where size is the size of the Bundle called value

Answer

andr picture andr · Jan 28, 2013

A Bundle is basically a dictionary. Each value in the Bundle is stored under a key. You must know the type of value under the key. When you know the type, you access the value associated with the key by calling a method relevant for the type of the value (again, you must know the type).

For example if the key is request and its type is String you would call:

String value = bundle.getString("request");

If the type was long, you would call:

long value = bundle.getLong("request");

To loop over the to array provided that the value is of type String you can do this:

for (int i = 0; bundle.containsKey("to[" + i + "]"); i++) {
    String toElement = bundle.getString("to[" + i + "]");
}

which does not rely on the size of the bundle object.

All the keys in a bundle and the type of value for each key should be provided in the Facebook API for Android. If you need further information on the Bundle object please look at the reference here.