简体   繁体   English

如何在Android中从bundle中提取值

[英]How to extract values from bundle in Android

While sending requests via Facebook_Android SDK , I get a bundle in return. 在通过Facebook_Android SDK发送请求时,我得到了一个包。 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]是一个字符串数组。 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 其中size是Bundle的大小,称为value

A Bundle is basically a dictionary. Bundle基本上是一本字典。 Each value in the Bundle is stored under a key . Bundle中的每个值都存储在一个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). 当您知道类型时,可以通过调用与值类型相关的方法来访问与key关联的值(同样,您必须知道类型)。

For example if the key is request and its type is String you would call: 例如,如果keyrequest并且其类型是String您将调用:

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

If the type was long , you would call: 如果类型很long ,你会打电话:

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

To loop over the to array provided that the value is of type String you can do this: 要遍历to数组,前提是该值的类型为String您可以这样做:

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. 它不依赖于bundle对象的大小。

All the keys in a bundle and the type of value for each key should be provided in the Facebook API for Android. 应在Facebook API for Android中提供捆绑中的所有密钥以及每个密钥的值类型。 If you need further information on the Bundle object please look at the reference here . 如果您需要有关Bundle对象的更多信息,请查看此处参考资料

Bundle bundle = intent.getBundle();
bundle.getString("ITEM_NAME");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM