1

Here is my AddLift.java:

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

    final EditText Origin = (EditText)findViewById(R.id.addOrigin);
    final EditText Destination = 
(EditText)findViewById(R.id.addDestination);
    final EditText Time = (EditText)findViewById(R.id.setTime);
    final EditText  Seats = (EditText)findViewById(R.id.numOfSeats);
    final RadioGroup DriverLifter = (RadioGroup)findViewById(driverLifter);
    final RadioButton selectedButton = 
(RadioButton)findViewById(selectedButton);

    Button submit = (Button)findViewById(R.id.submitButton);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String addOrigin = Origin.getText().toString();
            String addDestination = Destination.getText().toString();
            String setTime = Time.getText().toString();
            String numOfSeats = Seats.getText().toString();
            int selectedId = DriverLifter.getCheckedRadioButtonId();
            selectedButton = (RadioButton)findViewById(selectedId);

            Intent intent = new Intent(AddLift.this, ViewLiftBoard.class);
            intent.putExtra("ORIGIN", addOrigin);
            intent.putExtra("DESTINATION", addDestination);
            intent.putExtra("TIME", setTime);
            intent.putExtra("SEATS", numOfSeats);
            intent.putExtra("RADIO", driverLifter);
            startActivity(intent);

        }
    });

And here is my ViewLiftBoard.java:

public class ViewLiftBoard extends AppCompatActivity {

    String Origin;
    String Destination;
    String Time;
    String Seats;
    int DriverLifter;


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


        Origin = getIntent().getExtras().getString("ORIGIN");
        Destination = getIntent().getExtras().getString("DESTINATION");
        Time = getIntent().getExtras().getString("TIME");
        Seats = getIntent().getExtras().getString("SEATS");
        DriverLifter = getIntent().getExtras().getInt("RADIO");

        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText("Origin:"+ " 
        "+Origin+'\n'+"Destination:"+""+Destination+'\n'+"Time:"+" 
        "+Time+'\n'+"Number of Seats:"+" "+Seats+'\n'+"Type:"+" "+DriverLifter);

   }
}

But when I run the app the "Type" comes up as a number rather than the value of the selected radio button? Any help would be great

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Dylan
  • 83
  • 8
  • What is TYPE here. I don't see it passed to next activity. – am110787 Apr 11 '17 at 13:36
  • Possible duplicate of http://stackoverflow.com/questions/18179124/android-getting-value-from-selected-radiobutton Please check if this helps. – am110787 Apr 11 '17 at 13:37

3 Answers3

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

        final EditText Origin = (EditText)findViewById(R.id.addOrigin);
        final EditText Destination = 
    (EditText)findViewById(R.id.addDestination);
        final EditText Time = (EditText)findViewById(R.id.setTime);
        final EditText  Seats = (EditText)findViewById(R.id.numOfSeats);
        final RadioGroup DriverLifter = (RadioGroup)findViewById(driverLifter);
        final RadioButton selectedButton = 
    (RadioButton)findViewById(selectedButton);

        Button submit = (Button)findViewById(R.id.submitButton);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addOrigin = Origin.getText().toString();
                String addDestination = Destination.getText().toString();
                String setTime = Time.getText().toString();
                String numOfSeats = Seats.getText().toString();
                int selectedId = DriverLifter.getCheckedRadioButtonId();
                selectedButton = (RadioButton)findViewById(selectedId);
                String selectedradio=selectedButton.gettex();
                Intent intent = new Intent(AddLift.this, ViewLiftBoard.class);
                intent.putExtra("ORIGIN", addOrigin);
                intent.putExtra("DESTINATION", addDestination);
                intent.putExtra("TIME", setTime);
                intent.putExtra("SEATS", numOfSeats);
                intent.putExtra("RADIO", selectedradio);
                startActivity(intent);

            }
        });



ViewLiftBoard.java:


public class ViewLiftBoard extends AppCompatActivity {

    String Origin;
    String Destination;
    String Time;
    String Seats;
    String DriverLifter;


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


        Origin = getIntent().getExtras().getString("ORIGIN");
        Destination = getIntent().getExtras().getString("DESTINATION");
        Time = getIntent().getExtras().getString("TIME");
        Seats = getIntent().getExtras().getString("SEATS");
        DriverLifter = getIntent().getExtras().getString("RADIO");

        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText("Origin:"+ " 
        "+Origin+'\n'+"Destination:"+""+Destination+'\n'+"Time:"+" 
        "+Time+'\n'+"Number of Seats:"+" "+Seats+'\n'+"Type:"+" "+DriverLifter);

   }
}
0

That's because you are retrieving an Integer DataType from the intent extra

   DriverLifter= getIntent().getExtras().getInt("RADIO");

To solve your problem you have to put this extra as a string in the intent, so instead of doing this :

 intent.putExtra("RADIO", driverLifter);

Do this :

     intent.putExtra("RADIO",  (RadioButton) findValueById(DriverLifter .getCheckedRadioButton()).  getText(). toString()) ;

And extract the Extra like this :

 DriverLifter= getIntent().getExtras().getString("RADIO");
0

If you want to get text from selected radio button, you can use next combination:

RadioGroup driverLifter = (RadioGroup)findViewById(R.id.driverLifter);
String selectedButtonText = ((RadioButton)findViewById(driverLifter.getCheckedRadioButtonId())).getText().toString();

By the way, according to Java naming conventions, your variables should start from lower case.

Gaket
  • 6,533
  • 2
  • 37
  • 67