1

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

JaneWi.S
  • 17
  • 8
  • 1
    have you checked the docs. There is sample with code snippets. http://developer.android.com/training/basics/firstapp/starting-activity.html – Raghunandan Apr 19 '14 at 04:27
  • Yes,have read it through but by using intent it starts another activity.As I mentioned classB is just a simple java class with no activity. – JaneWi.S Apr 19 '14 at 04:32
  • You can use static variable in your first class and store that value which you want to send when you press next activity's button or image whatever. And then use that static variable over there.. – InnocentKiller Apr 19 '14 at 04:32
  • @JaneWi.S then just pass the value to the constructor of class b. – Raghunandan Apr 19 '14 at 04:33
  • 1
    Could you post some code, so we get an idea of what you need? – Christopher Francisco Apr 19 '14 at 04:42

4 Answers4

2

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());
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1

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);
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
0

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", "");
androidStud
  • 532
  • 4
  • 9
0

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; 
jyomin
  • 1,957
  • 2
  • 11
  • 27