简体   繁体   中英

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 ). 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).

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...

public static ClientList returnobj(){
    return Clist;
}

Back in ViewClient I called

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.

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 ,

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. 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,

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' ? Since ListView, Activity and so on are Android specific I'd say no.

But your 'Concept' should/could work without 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 ).

For graphical user interfaces in Java SE, you should a library like Swing or SWT .

If you're asking whether the general scheme of transferring data will work on Java SE, then yes, it will . The reason is exactly you using the static keyword. 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. That means that everywhere in your program, the value of the static member is the same, and returnobj() will return the same object.

Without the static keyword, the value would exist once for each object of showclients you create, and not the class as a whole.

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). 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.

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 . In Java SE you would implement a similar solution by creating a new class implementing the Singleton pattern . And, actually, MyApplication in Jeffrey's examples is a singleton for the purpose of your app.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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