简体   繁体   中英

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

1.classA has a editbox where the user inputs some value. 2.classB has a method that computes the user input.

need to get the user input value from classA to classB.

a).cannot get it through passing intent as the other class does not have activity. b).have tried getter setter method by creating a class thus,calling setter where the user inputs the detail. and calling the getter in another class.

Still the value is null, so what apparently is missing here.Any guidance to a example or brief explanation would be great.Thanks

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

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

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.

OR

Also you can use SharedPrefernces if you want to persist your data and store it for later use.

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*/
  }

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