3

I am trying to send information typed in an EditText of one activity to another activity, and then display it in a TextView.

The code which sends the information:

EditText ed = (EditText)findViewById(R.id.soeg_vare);
String s = ed.getText().toString();

public void onClick(View v)
{
    Intent myIntent = new Intent(Vare.this, Vare_resultat.class);
    myIntent.putExtra("SOEG_VARE",s);
    startActivity(myIntent);
}       

Code that receives the information:

Intent i = getIntent();
String var = i.getStringExtra("SOEG_VARE");
int convert = Integer.parseInt(var);

3 Answers 3

0

You should move this line inside the onClick method:

String s = ed.getText().toString();

Right now s is getting set the same time you initialize the editText and not getting looked at again when button is pushed.

0

There might be a probability where your EditText is still empty when you call

String s = ed.getText().toString();

Try this:

EditText ed = (EditText)findViewById(R.id.soeg_vare);

public void onClick(View v){
    String s = ed.getText().toString();
    Intent myIntent = new Intent(Vare.this, Vare_resultat.class);
    myIntent.putExtra(s, s);
    startActivity(myIntent);
}     

Code that receives information:

String s = "";
s = getIntent().getExtras().getString(s);
int convert = Integer.parseInt(s);
1
  • thanks for the answer! This activity is in another activity. I got activity "A", "B" and "C" and i need to transfer information between "B" and "C". But with this code when i try to access "B" from a button in "A" the app stop working. Without the code I can easy get to activity B.
    – Asger N.
    May 25, 2013 at 8:12
0

Solution by the original asker:

Code that sends:

private void addButtonListener() {
    final Button soeg = (Button) findViewById(R.id.soeg);
    final EditText ed = (EditText) findViewById(R.id.soeg_vare);
    soeg.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            Intent send_soeg = new Intent(Vare.this, Vare_resultat.class);
            String s = ed.getText().toString();
            send_soeg.putExtra("SEND_SØG", s);
            startActivity(send_soeg);
        }
    });
}

Code that receives:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vare_resultat);        
    TextView modtag_soeg = (TextView) findViewById(R.id.modtag_soeg);
    modtag_soeg.setText("Du søgte efter " + getIntent().getExtras().getString("SEND_SØG"));
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.