-1

I have 3 classes: 1- The main class 2- The list class 3- The calculating class

I want to use the main class to let the user input the values in EditText then use the second class to let the user select the method of calculation then use the third class to perform the calculations.

I need to know how to pass an EditText value between those three classes

4 Answers4

0

Use the putExtra method of Intent.
Example:

Intent i = new Intent(Main.this, List.class);
i.putExtra("name of variable", variable);
startActivity(i);
Fabian
  • 356
  • 1
  • 5
  • 15
  • t works well in two classes situation but in case of three classes it doesn't work. As the input class just get the text from the EditText then the second class (only alist class, so i can't pass values to the second i need to pass the values to the third while opening the second class !! – – user3049408 Nov 29 '13 at 13:36
0

Use this to "put" the file...

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
i.putExtra("STRING_I_NEED", edt.getText().toString);

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
    extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

For more information check the given link http://www.vogella.com/articles/AndroidIntent/article.html

Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
  • 1
    t works well in two classes situation but in case of three classes it doesn't work. As the input class just get the text from the EditText then the second class (only alist class, so i can't pass values to the second i need to pass the values to the third while opening the second class !! – user3049408 Nov 29 '13 at 13:37
0

Get the text from your EditText

EditText view = (EditText) findViewById(R.id.get_Id);

Pass your Edit text value to next activity like:

Intent intent = new Intent(context, YourActivity2.class);
intent.putExtra("selectedTrack", view.getText().toString());
startActivity(intent);
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • It works well in two classes situation but in case of three classes it doesn't work. As the input class just get the text from the EditText then the second class (only alist class, so i can't pass values to the second i need to pass the values to the third while opening the second class !! – user3049408 Nov 29 '13 at 13:36
0

All the above approaches are correct, but because you set EditText in Activity A, and using it in Activity C, you would have to pass EditText value to Activity B, and then from B pass both EditText value AND method of calculation. As an alternative, you could use SharedPreferences:

Activity A:

//get EditText's value, I will assume you want it to be an int 
//just to show you how to save it
//later, we're doing the same with String
//NOTE: no checks are done for correct input, the below will fail
//if myEditText's content is not valid to be converted to int
EditText myEditText = (EditText)findViewById(R.id.myEditText);

int value = Integer.parseInt(myEditText.getText().toString());

// We need an Editor object to make preference changes.
SharedPreferences settings = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("value", value);

// Commit the edits!
editor.commit();

Activity B: (I'm assuming that method is String, but it can be any other primitive type)

String method = "multiplication";

// We need an Editor object to make preference changes.
SharedPreferences settings = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("method", method);

// Commit the edits!
editor.commit();

Then, in Activity C, you can retrieve all those values:

// Restore preferences
SharedPreferences settings = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
int value = settings.getInt("value", 0);
String method = settings.getString("method", "division");

The second arguments for both the above is the default value, in case settings don't exist.

Please read the docs here and here for more info on SharedPreferences.

Melquiades
  • 8,496
  • 1
  • 31
  • 46