-1

Android - I am using intent to communicate between two activities. I used putExtra() in the calling activity and i am using getExtras in the called activity. But the problem is iam not able to set the edit text with the number i retrieved in the intent. below is my program`

// Calling activity

package com.example.androidtutorial2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.text.InputType;

public class MainActivity extends Activity {

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

        //EDIT TEXT
        final EditText sharedata = (EditText) findViewById ( R.id.editText1 ) ;
        sharedata.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

        //BUTTON
        Button sendbutton = (Button) findViewById (R.id.button2) ;
        sendbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int edittext_data =                     Integer.valueOf(sharedata.getText().toString());

                Intent sendintent = new Intent(MainActivity.this,second_activity.class);
                sendintent.putExtra("somedata", edittext_data);
                startActivity(sendintent);

            }
        });

//        Button button_to_call_activity = (Button) findViewById(R.id.button1);
//        button_to_call_activity.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//          
//              
//          }
//      });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}


//CALLED ACTIVITY



package com.example.androidtutorial2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;

public class second_activity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        EditText displaymessage = (EditText) findViewById ( R.id.editText1 );

        Intent intent1 = getIntent();
        Bundle bundle1 = intent1.getExtras();
        int integer1 = bundle1.getInt("somedata");

        Integer string1 = integer1;

        displaymessage.setText(integer1);               // HERE IS A RUNTIME ERROR


    }
}
GaryJJ
  • 13
  • 3

3 Answers3

3

use

displaymessage.setText("" +integer1); 

Or

displaymessage.setText(String.valueOf(integer1)); 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

1. First of all if you are using Java 1.5 and above dont useInteger.valueOf() but instead use Integer.parseInt().

eg:

int edittext_data = Integer.parseInt(sharedata.getText().toString());

2. Your problem with the output is here.

Dont do this ... Integer string1 = integer1;

Use it like this...

String string1 = getIntent().getExtras().getString("somedata"); displaymessage.setText(string1);

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Since you don't seem to be doing anything with the variable as an integer, you could also simply keep it as a string.

sendintent.putExtra("somedata", sharedata.getText().toString());

String integer1 = bundle1.getString("somedata");
leenephi
  • 900
  • 5
  • 13
  • Also, FYI, the reason you're getting an error putting an int into setText() is because setText(int) is meant to look for an int xml resource id. So, if you really wanted to put it in as an int, do what imran said above: setText("" + int); – leenephi Jul 21 '12 at 19:22