JaneWi.S
JaneWi.S

Reputation: 17

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

Upvotes: 1

Views: 2538

Answers (4)

Pankaj Arora
Pankaj Arora

Reputation: 10274

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

Upvotes: 1

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

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());

Upvotes: 2

jyomin
jyomin

Reputation: 1967

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; 

Upvotes: 0

androidStud
androidStud

Reputation: 532

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", "");

Upvotes: 0

Related Questions