[英]An inquiry regarding Java classes objects and data transfer from one activity to another in Android
So I'm making an Android app and I've got one ListView
(called showclients
) that links to another (called ViewClient
). 因此,我正在制作一个Android应用程序,并且有一个
ListView
(称为showclients
)链接到另一个ListView
(称为ViewClient
)。 When showclients
is called, it gathers a bunch of data from other java classes and stores it in an object of type ClientList
(which I've made). 调用
showclients
,它将从其他Java类中收集一堆数据并将其存储在ClientList
类型的对象中(我已经制作了)。
ViewClient
needs that info and it takes way too long to get it from scratch again, so in showclients
I made a public static
variable called Clist
of type ClientList
and made a method... ViewClient
需要该信息,并且花费太长时间才能再次从头开始获取信息,因此在showclients
我创建了一个名为Clist
的ClientList
类型的public static
变量,并创建了一个方法...
public static ClientList returnobj(){
return Clist;
}
Back in ViewClient
I called 回到
ViewClient
我打电话给
ClientList grief= showclients.returnobj();
and it gets me the data. 它得到了我的数据。 I want to know why that works and if it'll work on Java without Android.
我想知道为什么它能工作,以及是否可以在没有Android的Java上工作。
so you are asking how to share data between activities. 所以您要问如何在活动之间共享数据。 one option is to host your data model in your application class.
一种选择是将数据模型托管在应用程序类中。 create a class that extends
Application
, 创建一个扩展
Application
的类,
public MyApplication extends Application { ...
in your manifest, add your application class, 在清单中添加您的应用程序类,
<application
android:name=".MyApplication"
...
the lifecycle of the Application
class allows you to host your data there. Application
类的生命周期允许您在此处托管数据。 it lives beyond the starting / stopping on any particular activity. 它超出了任何特定活动的开始/停止的范围。 add private members to your
MyApplication
class, and provide getters and setters to access the data, 将私有成员添加到
MyApplication
类,并提供获取器和设置器以访问数据,
public MyApplication extends Application {
private MyData myData;
public MyData getMyData() { return myData; }
public void setMyData(MyData data) { this.myData = data; }
}
What exactly is your Problem? 你到底是什么问题?
What means 'Java without Android' ? 什么意思是“没有Android的Java”? Since ListView, Activity and so on are Android specific I'd say no.
由于ListView,Activity等是特定于Android的,因此我拒绝。
But your 'Concept' should/could work without Android. 但是您的“概念”应该/可以在没有Android的情况下工作。 -> Please more Info...
->请更多信息...
IMHO your description is a bit cryptic... maybe post some more code. 恕我直言,您的描述有点含糊...也许会发布更多代码。
If you're asking whether this exact code will work by compiling and running it on Java SE (ie "without Android"), then no , since Java SE does not have some classes that you use (like Activity
). 如果您要问这个确切的代码是否可以通过在Java SE上编译并运行它(即“没有Android”)来工作,那么请否 ,因为Java SE没有使用某些类(例如
Activity
)。
For graphical user interfaces in Java SE, you should a library like Swing or SWT . 对于Java SE中的图形用户界面,您应该使用Swing或SWT之类的库。
If you're asking whether the general scheme of transferring data will work on Java SE, then yes, it will . 如果您要问通用的数据传输方案是否可以在Java SE上运行,那么可以 。 The reason is exactly you using the
static
keyword. 原因恰恰是您使用
static
关键字。 This means that the static "variable" (actually a member ) and the static method is defined for the entire showclients
class, and not any of its objects in particular. 这意味着静态的“变量”(实际上是member )和静态方法是为整个
showclients
类定义的,而不是为其特定对象定义的。 That means that everywhere in your program, the value of the static member is the same, and returnobj()
will return the same object. 这意味着在程序的任何地方,静态成员的值都是相同的,并且
returnobj()
将返回相同的对象。
Without the static
keyword, the value would exist once for each object of showclients
you create, and not the class as a whole. 如果没有
static
关键字,则对于您创建的showclients
每个对象,该值将只存在一次,而不是整个类。
That's one of the reasons why your solution works on Android - if the value would not be stored in a static member, you would have to reference the actual showclients
object, which could get removed from memory at any time while your other Activity is showing (because a background Activity, showclients
in this case, can be removed at any time). 这就是您的解决方案在Android上运行的原因之一-如果该值不会存储在静态成员中,则您必须引用实际的
showclients
对象,该对象可能会在其他活动显示时随时从内存中删除(因为可以随时删除后台活动(在这种情况下为showclients
)。 Because the member is static, it hangs around as long as showclients
is loaded by the Java Virtual Machine, which for your purposes is as long as you need. 因为成员是静态的,所以只要Java虚拟机加载了
showclients
,它就会一直showclients
,这对于您来说是您需要的时间。
Finally, Jeffrey's solution is much better for Android code, since you're not abusing the definition of an Activity
for additional functionality that should be placed in a dedicated piece of code . 最后, Jeffrey的解决方案对于Android代码而言要好得多,因为您不会滥用
Activity
的定义来获取应放在专用代码段中的其他功能。 In Java SE you would implement a similar solution by creating a new class implementing the Singleton pattern . 在Java SE中,您将通过创建实现Singleton模式的新类来实现类似的解决方案。 And, actually,
MyApplication
in Jeffrey's examples is a singleton for the purpose of your app. 而且,实际上,就您的应用而言,Jeffrey示例中的
MyApplication
是单例。
I think you are asking about static keyword? 我认为您正在询问静态关键字? Static methods (or variables) are bound directly to class itself, not its particular instance.
静态方法(或变量)直接绑定到类本身,而不是其特定实例。 It's better to show on example, but with variable (do not use it this way, read about encapsulation):
最好在示例中显示,但是带有变量(不要以这种方式使用,请阅读封装):
class A {
public static String foo = "foo";
public A() {
foo = "bar";
}
}
class B {
public void something() {
// suppose print exists
print(A.foo); // prints foo
A a = new A();
print(A.foo); // prints bar
print(a.foo); // prints bar
}
}
Make yourself some simple java application (that's the one w 使自己成为一些简单的Java应用程序(就是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.