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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:39:45  来源:igfitidea点击:

How to extract values from bundle in Android

javaandroidfacebook-android-sdk

提问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 sizeis the size of the Bundle called value

sizeBundle 的大小在哪里称为value

回答by andr

A Bundleis 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 keyby calling a method relevant for the type of the value (again, you must know the type).

ABundle基本上是一本字典。Bundle 中的每个值都存储在一个key. 您必须知道键下的值类型。当您知道类型时,您可以key通过调用与值的类型相关的方法来访问与 关联的值(同样,您必须知道类型)。

For example if the keyis requestand its type is Stringyou would call:

例如,如果keyisrequest和它的类型是String你会调用:

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

If the type was long, you would call:

如果类型是long,您将调用:

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

To loop over the toarray provided that the value is of type Stringyou 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 Bundleobject please look at the reference here.

捆绑包中的所有键和每个键的值类型都应在 Facebook API for Android 中提供。如果您需要有关该Bundle对象的更多信息,请查看此处参考资料

回答by Samee Mir

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