简体   繁体   English

从一个活动获得价值到另一个活动

[英]Getting value from one activity to another class

can anyone guide me to the right direction, as the situation is have two classes calssA with Activity while classB is simple java class 任何人都可以指导我到正确的方向,因为情况有两个类calssA与Activity而classB是简单的java类

1.classA has a editbox where the user inputs some value. 1.classA有一个编辑框,用户输入一些值。 2.classB has a method that computes the user input. 2.classB有一个计算用户输入的方法。

need to get the user input value from classA to classB. 需要从classA到classB获取用户输入值。

a).cannot get it through passing intent as the other class does not have activity. a)。由于其他类没有活动,因此无法通过传递意图获得它。 b).have tried getter setter method by creating a class thus,calling setter where the user inputs the detail. b)。通过创建一个类来尝试getter setter方法,调用setter,用户输入详细信息。 and calling the getter in another class. 并在另一个类中调用getter。

Still the value is null, so what apparently is missing here.Any guidance to a example or brief explanation would be great.Thanks 仍然是值为null,所以这里显然缺少。对示例或简要解释的任何指导都会很棒。谢谢

You can use your own customized class to store the data and use it any where you need. 您可以使用自己的自定义类来存储数据,并在您需要的任何地方使用它。

DataClass 数据类

public class Data {

       private String a,b;
   private static Data data= new Data( );

   /* A private Constructor prevents any other 
    * class from instantiating.
    */
   private Data (){ }

   /* Static 'instance' method */
   public static Data getInstance( ) {
      return data;
   }

       public String getA()
       {
          return this.a;
       }

       public String getB()
       {
          return this.b;
       }

       public void setA(String a)
       {
          this.a = a;
       }

       public String getB(String b)
       {
          this.b = b;
       }
  }

In Activity 在活动中

Data data = Data.getInstance()

data.setA("Stack");
data.setA("Overflow");

you can use this values in Java file like this 您可以像这样在Java文件中使用此值

Data data = Data.getInstance()

System.out.println(data.getA());
System.out.println(data.getB());

according to my knowledge here you have to use singleton class. 根据我的知识,你必须使用单身类。

public class DataHolderClass {
private static DataHolderClass dataObject = null;

private DataHolderClass() {
    // left blank intentionally
}

public static DataHolderClass getInstance() {
    if (dataObject == null)
        dataObject = new DataHolderClass();
    return dataObject;
}

private String _ProductNames;

public String get_ProductNames() {
    return _ProductNames;
}

public void set_ProductNames(String _ProductNames) {
    this._ProductNames = _ProductNames;
}
}

to set data 设置数据

DataHolderClass.DataHolderClass.set_ProductNames(your data variable);

to get data 获取数据

DataHolderClass.DataHolderClass.get_ProductNames(your data variable);

You can save the edittext value in SharedPreferences, thus making it available in all activity. 您可以将edittext值保存在SharedPreferences中,从而使其在所有活动中都可用。 By doing this you can fetch the value in other activity without any hassle. 通过这样做,您可以毫无困难地获取其他活动中的值。

eg: 例如:

SharedPreferences.Editor editor = preferences.edit();
editor.putString("edtTextValue", valueOfEditText);
editor.commit();

Further fetching the value in other activity like this, 进一步获取此类其他活动的值,

preferences.getString("edtTextValue", "");

In your activity class create 在您的活动类中创建

public static String valueEntered;
valueEntered=mEdtTxt.getText().toString();

and in your java class where you want 并在您想要的java类中

String enteredValue=ClassA.valueEntered; 

User a Class to hold your value 用户一个类来保持你的价值

public class ValueHolderClass {

    public static String valueYouWantToStore_arg;

}

in Activity Class 在活动类中

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText ed=(EditText)findViewById(R.id.myedittext);
        valueYouWantToStore_arg= ed.getText().toString().trim();

    }

you can use this when you dont want to persist your data. 如果您不想保留数据,可以使用此功能。 you can also refer to Ram kiran answer which is a better way. 你也可以参考Ram kiran的答案,这是一个更好的方法。

OR 要么

Also you can use SharedPrefernces if you want to persist your data and store it for later use. 如果要保留数据并将其存储以供以后使用,也可以使用SharedPrefernces。

To store values in shared preferences: 要在共享首选项中存储值:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("Name","Harneet");
  editor.commit();

To retrieve values from shared preferences: 要从共享首选项中检索值:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String name = preferences.getString("Name","");
  if(!name.equalsIgnoreCase(""))
  {
    name = name+"  Sethi";  /* Edit the value here*/
  }

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

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