黑莓手机 - 如何通过像Intent.putExtra()在Android的数据 [英] BlackBerry - how to pass data like Intent.putExtra() in Android
问题描述
我是新手的黑莓手机。我如何实现(黑莓)相同Android的的
I am newbie on BlackBerry. How do I achieve (in BlackBerry) the same as that of Android's
intent.putExtra("key","value");
将可在下次接收值的推屏幕的黑莓。
就像机器人从 ActivityOne
-
Like in Android from ActivityOne
-
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("Value1", "This value one for ActivityTwo ");
intent.putExtra("Value2", "This value two ActivityTwo");
startActivity(intent);
在 ActivityTwo
-
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}
时的黑莓可能这样的事情?如果是的话我怎么做呢?
Is such a thing possible in BlackBerry? If yes then how do I do this?
推荐答案
使用公共静态
变量(如Ganesh神的答案)会工作,但在一般情况下,这不是一个面向对象的模式,我会推荐。
Using public static
variables (as in Ganesh's answer) will work, but in general, that's not an object-oriented pattern that I would recommend.
幸运的是,这的可以的更容易在黑莓手机比Android系统。 Android的意图
机制实际上是有些不自然了许多Java开发人员。当一个活动
启动另一个活动
通过意图
,它实际上并不新
达(创建)第二个活动
,也未持有正常的实例Java引用到该对象。如果有,这个问题就会简单得多。 Android的实施强迫你使用意图演员机制。
Luckily, this can be easier in BlackBerry than in Android. Android's Intent
mechanism is actually somewhat unnatural to many Java developers. When one Activity
starts another Activity
via an Intent
, it does not actually new
up (create) the instance of the second Activity
, and it also doesn't hold a normal Java reference to that object. If it did, this problem would be much simpler. Android's implementation forces you to use the intent extras mechanism.
如果您的旧 ActivityOne
类成为 ScreenOne
黑莓,和 ActivityTwo
变成 ScreenTwo
,那么你可以做这样的事情:
If your old ActivityOne
class becomes ScreenOne
for BlackBerry, and ActivityTwo
becomes ScreenTwo
, then you can just do something like this:
public class ScreenTwo extends Screen {
private String _value1; // this doesn't have to be a String ... it's whatever you want
private String _value2;
public void setValue1(String value) {
_value1 = value;
}
public void setValue2(String value) {
_value2 = value;
}
}
然后,在 ScreenOne
,你就可以开始 ScreenTwo
这样
Then, in ScreenOne
, you can start ScreenTwo
this way
ScreenTwo nextScreen = new ScreenTwo();
nextScreen.setValue1("This value one for ActivityTwo");
nextScreen.setValue2("This value two ActivityTwo");
UiApplication.getUiApplication().pushScreen(nextScreen);
这实际上与Java对象正常的使用方式更加一致,并与人交往。
That's actually more consistent with the way Java objects normally are used, and interact with one another.
有充分的理由的Android取得了意图
和其他,但在黑莓,你根本不必担心。
There's good reasons why Android made Intents
and extras, but in BlackBerry, you just don't have to worry about that.
编辑:我想考虑一下我的认为的背后是低于史密斯先生的评论的动机。如果你真的喜欢Android的意图
演员机制在这个意义上,你可以从一个活动传递多种数据类型
到另一个作为键值对,那么你一定可以实现黑莓类似的东西。取而代之的是 ScreenTwo
code以上,你可以使用这样的:
I'm trying to consider what I think is the motivation behind Mr. Smith's comment below. If you actually like the Android Intent
extras mechanism in the sense that you can pass multiple data types from one Activity
to another, as key-value pairs, then you can certainly achieve something similar in BlackBerry. Instead of the ScreenTwo
code above, you could use this:
public class ScreenTwo extends Screen {
private Hashtable _extras;
public void putExtras(Hashtable extras) {
_extras = extras;
}
}
您放(对象,对象)
键 - 值对数据转换为的Hashtable
传递到调用的画面,然后读取它,当你需要它。甚至:
Where you put(Object, Object)
key-value pair data into a Hashtable
passed to the called screen, and then read it when you need it. Or even:
public class ScreenTwo extends Screen {
private Hashtable _extras;
public void putExtra(String name, Object value) {
_extras.put(name, value);
}
}
这篇关于黑莓手机 - 如何通过像Intent.putExtra()在Android的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!