java 如何从Android中的bundle中提取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14556707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract values from bundle in Android
提问by saran
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.
通过Facebook_Android SDK发送请求时,我得到了一个包作为回报。有人可以解释一下它是什么数据类型以及如何提取其中的数据吗?谢谢。
01-28 11:58:07.548: I/Values(16661): Bundle[{to[0]=100005099741441, to[1]=100005089509891, request=134129756751737}]
01-28 11:58:07.548: I/Values(16661): Bundle[{to[0]=100005099741441, to[1]=100005089509891, request=134129756751737}]
EDITHere, to[i] is a string array. I was able to do it. but I don't think its the right way to do it.
编辑这里, to[i] 是一个字符串数组。我能够做到。但我认为这不是正确的做法。
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
回答by andr
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).
ABundle
基本上是一本字典。Bundle 中的每个值都存储在一个key
. 您必须知道键下的值类型。当您知道类型时,您可以key
通过调用与值的类型相关的方法来访问与 关联的值(同样,您必须知道类型)。
For example if the key
is request
and its type is String
you would call:
例如,如果key
isrequest
和它的类型是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.
这不依赖于包对象的大小。
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.
捆绑包中的所有键和每个键的值类型都应在 Facebook API for Android 中提供。如果您需要有关该Bundle
对象的更多信息,请查看此处的参考资料。
回答by Samee Mir
Bundle bundle = intent.getBundle();
bundle.getString("ITEM_NAME");