-1

I tried to transfer data from activity to another activitiy but I got error from eclipse:

The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, EditText)

please help me

public class MainActivity extends Activity {
EditText text1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.submit);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             text1   = (EditText)findViewById(R.id.editText1);


                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                intent.putExtra("text1",text1);
                //start the second Activity
                startActivity(intent);

        //   Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
        }
    });
}



@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return true;
} 

}

Alona
  • 27
  • 1
  • 8
  • why you want to transfer a edittext to another activity ? because only some special kind of data can be carry by intent . such like string , int ,boolean and something extends parcelable or something that can be serializable . hope that helps – cowboi-peng Jul 04 '14 at 01:42
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Dhaval Parmar Jul 04 '14 at 05:52

4 Answers4

1

I think what you're looking for is

intent.putExtra("text1",text1.getText().toString());

To pass something like weight and height might look like this:

String weight = text1.getText().toString(); // where text1 is the EditText with user's weight
String height = text2.getText().toString(); // where text2 is the EditText with user's height

intent.putExtra("WEIGHT", weight); 
intent.putExtra("HEIGHT", height);
bwegs
  • 3,769
  • 2
  • 30
  • 33
0

You can pass strings and primitive data types between actvities but not UI components between two actvities. http://developer.android.com/reference/android/content/Intent.html

  • i am trying to build a bmi calculator. in the main activity i built a form which asks the user for his weight and height. now I have to transfer this data (weight, height) to a new "page" and calculate and write the result... can't I do it by intent? – Alona Jul 04 '14 at 01:48
0

You can pass the String as follows

intent.putExtra("text1",text1.getText().toString());

But you can not pass UI components such as EditText, ....

  Button button = (Button) findViewById(R.id.submit);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             text1   = (EditText)findViewById(R.id.editText1);


                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                intent.putExtra("text1",text1.getText().toString());
                //start the second Activity
                startActivity(intent);

        //   Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
        }
    });
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • I tried it didn't work because there is 2 dots " ()..toString " .. member "Bmw14" corrected it.. thanks for help anyway :) – Alona Jul 04 '14 at 02:13
0

I see what you want to do, you want the text from that EditText dont you?

Then do this..

public void onClick(View v) {
         text1   = (EditText)findViewById(R.id.editText1);
         String desiredtext=text1.getText();


            Intent intent = new Intent(MainActivity.this,SecondActivity.class);
            intent.putExtra("text1",desiredText);
            //start the second Activity
            startActivity(intent);

    //   Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
    }
Ringo
  • 850
  • 9
  • 24