简体   繁体   English

将数据从一类传递到另一类

[英]Passing data from one class to another

In class B, I have the following method: 在B类中,我有以下方法:

public String sendPlayerDataMethod() {
  String testt = "thissss is for testing";
  /*** 
  Intent sendPlayerData = new Intent(this, FullPlayerList.class);
  sendPlayerData.putExtra(Intent.EXTRA_TEXT, testt);
  startActivity(sendPlayerData);
  ***/
 return testt;
}

In class A, I want to simply call this method. 在A类中,我只想简单地调用此方法。 I thought ClassB.sendPlayerDataMethod() would do the trick, but when I'm typing it out IntelliJ doesn't seem to pick it up. 我以为ClassB.sendPlayerDataMethod()可以解决问题,但是当我键入它时,IntelliJ似乎没有ClassB.sendPlayerDataMethod()

Both classes are public as well... 这两堂课都是公开的...
I want to call that method and then assign the return value to a var in ClassA... 我想调用该方法,然后将返回值分配给ClassA中的var。

Thanks 谢谢

Your method is actually correct, there's no problem with it. 您的方法实际上是正确的,没有问题。 The most likely reason you're still getting this behaviour is because of the way you are referencing the method (from class A). 您仍然出现此行为的最可能原因是由于您引用该方法的方式(来自A类)。 There are two approaches to solving this problem: 有两种方法可以解决此问题:

  • The first approach (and also the faster one) is to make your sendPlayerDataMethod() static and then referencing it like this in class A: 第一种方法(也是更快的方法是使sendPlayerDataMethod()静态,然后在类A中像这样引用它:

     ClassB.sendPlayerDataMethod(); 
  • The other option is to make it non-static. 另一种选择是使其为非静态。 In this case, you have to create an object of class B before you can use the method. 在这种情况下,必须使用该方法创建一个B类的对象。 Like this: 像这样:

     ClassB b = new ClassB(); b.sendPlayerDataMethod(); 

I hope this helps.. Merry coding! 希望对您有所帮助。

Sounds like you want to pass information from one activity to another activity. 听起来您想将信息从一个活动传递到另一个活动。

The common way is to create an Intent in class A (like in your commented code) and receive the intent by accessing the information about onCreate() method from savedInstance in class B. 常见的方法是在类A中创建一个Intent(就像在您的注释代码中一样),并通过从类B中的savedInstance访问有关onCreate()方法的信息来接收该意图。

Don't code such dirty tricks like static attributes and stuff. 不要编写诸如静态属性和内容之类的肮脏技巧。 Follow the Activity Lifecycle . 遵循活动生命周期

public class ActivityA extends Activity {
    ...
    public void sendPlayerDataMethod() {
        String test = "thissss is for testing";
        Intent sendPlayerData = new Intent(this, ActivityB.class);
        sendPlayerData.putExtra(Intent.EXTRA_TEXT, test);
        startActivity(sendPlayerData);
    }
    ...
}

public class ActivityB extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        String test = savedInstanceState.getString(Intent.EXTRA_TEXT);
        Log.i("MyApp", test); // log your String in LogCat
    }

}

(This code is just developed right here in SO without IDE so no guarantee) (此代码只是在没有IDE的情况下在此处开发的,因此无法保证)

In class A: 在A类中:

  • Non-static way: 非静态方式:

     B b = new B(); String data = b.sendPlayerDataMethod(); 
  • Static way: 静态方式:

     String data = B.sendPlayerDataMethod(); // Set the sendplayerdatamethod to static 

Make sure that the method is static like this: 确保该方法是静态的,如下所示:

public static String sendPlayerDataMethod() {
 String testt = "thissss is for testing";
 /*** Intent sendPlayerData = new Intent(this, FullPlayerList.class);
  sendPlayerData.putExtra(Intent.EXTRA_TEXT, testt);
  startActivity(sendPlayerData);***/
 return testt;
}

Then you can call it from class A like this: 然后可以从A类调用它,如下所示:

classB.sendPlayerDataMethod();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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