2

Okay, I simply want to have multiple intents passing data into a single activity which will summarise all the user inputs into a nice easy to read form.

This is for a mock up school application form.

The following code works fine but will produce two activities: One showing the student's gender and one showing the students name. How to get it to display both in the same activity? Pulling my hair out over this!!

XML of studentInfoActivity:

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
tools:context=".studentInfoActivity">

<TextView
    android:id="@+id/textView4"
    android:layout_width="143dp"
    android:layout_height="42dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="90dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:gravity="center"
    android:text="Student Full Name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="143dp"
    android:layout_height="42dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:gravity="center"
    android:text="Gender"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />

XML of reviewScreenActivity:

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".reviewScreenActivity">

<TextView
    android:id="@+id/textView15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="SUMMARY:"
    android:textSize="30sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/summaryStudentFamilyName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="150dp"
    android:layout_marginRight="150dp"
    android:text="The student's name is: "
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView15" />

<TextView
    android:id="@+id/summaryStudentGender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="68dp"
    android:layout_marginEnd="150dp"
    android:layout_marginRight="150dp"
    android:text="The student's gender is: "
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView15" />

<TextView
    android:id="@+id/studentFullNameReviewScreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="85dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/summaryStudentFamilyName"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/studentGenderReviewScreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/summaryStudentGender"
   app:layout_constraintTop_toBottomOf="@+id/studentFullNameReviewScreen" />
</android.support.constraint.ConstraintLayout>

Java code of studentInfoActivity:

public class studentInfoActivity extends AppCompatActivity {

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

    //TAKEN FROM https://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-list.
    //AUTHOR: NICOLAS TYLER
    //get the spinner from the xml.
    Spinner dropdown = findViewById(R.id.genderSpinner);
    //create a list of items for the spinner.
    String[] items = new String[]{"Male", "Female", "Other"};
    //create an adapter to describe how the items are displayed, adapters are used in several places in android.
    //There are multiple variations of this, but this is the basic variant.
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
    //set the spinners adapter to the previously created one.
    dropdown.setAdapter(adapter);

    //TAKEN FROM https://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-list.
    //AUTHOR: NICOLAS TYLER
    //get the spinner from the xml.
    Spinner atsiDropdown = findViewById(R.id.atisSpinner);
    //create a list of items for the spinner.
    String[] atsiItems = new String[]{"Yes", "No"};
    //create an adapter to describe how the items are displayed, adapters are used in several places in android.
    //There are multiple variations of this, but this is the basic variant.
    ArrayAdapter<String> atsiAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, atsiItems);
    //set the spinners adapter to the previously created one.
    atsiDropdown.setAdapter(atsiAdapter);
}
public void createCaregiverInfoSplash(View v){
    //Send Student Full Name
    EditText studentFullNameInput = (EditText) findViewById(R.id.studentFullNameInput);
    String studentFullName = studentFullNameInput.getText().toString();
    Intent studentIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
    studentIntent.putExtra("studentFullName", studentFullName);
    startActivity(studentIntent);

    //Send Student Gender
    Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
    String studentGender = genderSpinner.getSelectedItem().toString();
    Intent genderIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
    genderIntent.putExtra("studentGender", studentGender);
    startActivity(genderIntent);
}

}

Java code of reviewScreenActivity:

public class reviewScreenActivity extends AppCompatActivity {

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

    //Student Full Name reciever
    Bundle sfnBundle = getIntent().getExtras();
    String studentFullName = sfnBundle.getString("studentFullName");
    if (studentFullName != null) {
        TextView sfn = (TextView) findViewById(R.id.studentFullNameReviewScreen);
        sfn.setText(studentFullName);
    }
    else {
        TextView sfn = (TextView) findViewById(R.id.studentFullNameReviewScreen);
        sfn.setText("Not showing right!");
    }


    //Student Gender Reciever
    Bundle sgBundle = getIntent().getExtras();
    String studentGender = sgBundle.getString("studentGender");
    TextView sg = (TextView)findViewById(R.id.studentGenderReviewScreen);
    sg.setText(studentGender);
}

}

1 Answers1

0

Well you have a lot of mistakes such as incorrect class and method names.

You can pass as many as you want extra in one intent.

public void createCaregiverInfoSplash(View v){
    //Send Student Full Name
    EditText studentFullNameInput = (EditText) findViewById(R.id.studentFullNameInput);
    String studentFullName = studentFullNameInput.getText().toString();

    Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
    String studentGender = genderSpinner.getSelectedItem().toString();

    Intent reviewIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
    reviewIntent.putExtra("studentFullName", studentFullName);
    reviewIntent.putExtra("studentGender", studentGender);
    startActivity(studentIntent);
}
Rasul
  • 727
  • 7
  • 20