简体   繁体   English

在非活动类与Android活动类之间传递变量

[英]Pass variable between non-activity class to android activity class

I need to pass a variable from (around)non-activity class to an nameget(android activity)class . 我需要将变量从(非活动类)传递到nameget(android活动)类。 Display the passed variable value in text view.. Please tel me what needs to do from this example. 在文本视图中显示传递的变量值。.请告诉我此示例需要做什么。 How to pass it to android activity 如何将其传递给android活动

public class around(non-activity class)
{
    String name = "arjun";
    //how to pass this name value to an below activity
    nameget nam = new nameget();
    String new = nam.get(name);
}

public class nameget extends Activity(android activity class)
{
   public String get(String name)
   {
      String got = name;
      TextView t1 = (TextView)findViewById(R.id.textView1);
      t1.setText(name);
    }
}

Try this 尝试这个

  public class around(non-activity class)
    {
     public static  String name = "arjun";
        //how to pass this name string to an below activity
    }

    public class nameget extends Activity(android activity class)
    {
        TextView t1 = (TextView)findViewById(R.id.textView1);
//your class name around
        t1.setText(around.name);
    }

Try this, declare your non activity class in you activity class. 尝试此操作,在您的活动类中声明您的非活动类。

 public class around(non-activity class)
{
Public static String name;
 name = "arjun";
//how to pass this name string to an below activity
}

 public class nameget extends Activity(android activity class)
  {
  around ar = new around();
  //declare non activity class here

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
TextView t1 = (TextView)findViewById(R.id.textView1);
t1.setText(ar.name);
 }
 }

You can simply generate getter and setter for your variable in non-activity class like.. 您可以简单地在非活动类中为变量生成getter和setter。

public String getName(){
  return name;
}

public void setName(String name){
   this.name = name;
}

now from anywhere you can get/set value for name as.. 现在,您可以从任何地方获取/设置名称as的值。

Arround ar = new Arround()
ar.setName("Aruva"); //To set name
ar.getName();  // To get 

In your Activity do like.. 在您的活动中喜欢..

 t1.setText(ar.getName().toString());

And your non-activity class can be created anywhere either in same package or in another package in src folder.. 您的非活动类可以在同一软件包中或在src文件夹中的其他软件包中的任何位置创建。

Quite a strange question because your design is not clear here. 相当奇怪的问题,因为此处的设计不清楚。 You pass parameters to activity via intent bundle. 您可以通过意图包将参数传递给活动。 You can only pass them when starting activity. 您只有在开始活动时才能通过它们。 Activity can be only started on some Context object (in 99% cases other Activity). 活动只能在某些Context对象上启动(在99%的情况下是其他Activity)。

You can start activity from some non-activity class as long as you have access to context there. 您可以从某个非活动类开始活动,只要您可以在那里访问上下文即可。 Activity that you will start needs to know who is it's parent in case of back button press. 在按下后退按钮的情况下,您将要开始的活动需要知道谁是父母。

When you want to pass data to activity while starting it see: How do I pass data between Activities in Android application? 如果要在启动时将数据传递给活动,请参阅: 如何在Android应用程序的“活动”之间传递数据?

When it's not this case - please describe your case what is your non-activity class and how is it related to activity that you want pass data to. 如果不是这种情况,请说明您的情况是什么是非活动类,以及它与要将数据传递到的活动之间的关系。

the best approach for such problems is having an event drivent architecture. 解决此类问题的最佳方法是采用事件驱动架构。 you can use any event library, I would suggest eventBus . 您可以使用任何事件库,建议使用eventBus

(in scope of eventbus) create a sticky event called NameChangedEvent with a name property (在eventbus的范围内)使用名称属性创建一个名为NameChangedEvent的粘性事件

your activity will listen to it while active (on resume - on pause). 活动时(恢复-暂停时),您的活动将监听它。 when name is changed, you'll dispatch this event eg 名称更改时,您将分派此事件,例如

EventBus.getDefault().post(new NameChangedEvent(name));

your activity will catch this and update the view. 您的活动将抓住这一点并更新视图。 it can also read the latest version of this event onCreate method. 它也可以读取此事件的最新版本onCreate方法。

this is of course an overkill if you are trying to do this just for 1 field. 如果您仅尝试1个字段,这当然是一个过大的杀伤力。 but many apps need to handle data changes and update their UI and having events is the best way ( imho ) to do it. 但是许多应用程序需要处理数据更改并更新其UI,并且拥有事件是实现此目的的最佳方法( imho )。 this helps you loosely couple your app logic and UI. 这可以帮助您轻松地将应用逻辑和UI结合在一起。

Is more easy if you save a variable of type "Class" 如果保存类型为“ Class”的变量,则更容易

Example : (have fun) 示例:(玩得开心)

Class<?> MyClassSave;

MyClassSave = MainActivity.class;

And you can get all values from .class 您可以从.class获取所有值

you can make public/private (static), or pass for bundle / serializable / Obj 您可以公开/私有(静态),也可以通过捆绑/可序列化/ Obj

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

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