Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract values from bundle in Android

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

like image 539
saran Avatar asked Jan 28 '13 06:01

saran


People also ask

How do I get bundle data on Android?

Here's how we get the object passed by a Bundle: Intent intent=getIntent(); // Instantiate a Bundle Bundle bundle=intent. getExtras(); //Get data inside Persion Persion persion= (Persion) bundle. getSerializable("persion"); text_show.

How bundle works android?

Android Bundle is used to pass data between activities. The values that are to be passed are mapped to String keys which are later used in the next activity to retrieve the values. Following are the major types that are passed/retrieved to/from a Bundle.

What does bundle mean in android?

An Android App Bundle is a publishing format that includes all your app's compiled code and resources, and defers APK generation and signing to Google Play.


2 Answers

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.

like image 51
andr Avatar answered Oct 28 '22 22:10

andr


Bundle bundle = intent.getBundle();
bundle.getString("ITEM_NAME");
like image 26
Samee Mir Avatar answered Oct 28 '22 22:10

Samee Mir