Home > other >  Android Studio: How do I get user input (From Spinner and Edit Text) from activity to another activi
Android Studio: How do I get user input (From Spinner and Edit Text) from activity to another activi

Time:07-18

I am trying to get user input (from both Edit Text and Spinner) from RegisterActivity.Java into ListActivity.Java. The following code below is from RegisterActivity.Java. I only know how to launch the button to a new activity, but I do not know the correct way without error to get user data input from one activity to another.

---Java RegisterActivity.Java

    package com.example.fitnessassessmentcell;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    
    public class RegisterActivity extends AppCompatActivity {
        public Button button;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
    
            /*
    -------------------------------------"VERIFY" BUTTON------------------------------------------
             */
            button = (Button) findViewById(R.id.verify);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent d = new Intent(RegisterActivity.this, ListsActivity.class);
                    startActivity(d);
                    }
            });
            /*
    -------------------------------------DROP DOWN MENU FOR GENDER------------------------------------------
             */
    
            /*
            Define spinner object, look up same id declared in activity_register.xml
             */
            Spinner spinnergender=findViewById(R.id.spinner_gender);
              /*
            Create arrayadapter because it rendering every item in the languages string array to the screen
            when Java dropdown menu
             */
            ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this, R.array.gender, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
            spinnergender.setAdapter(adapter);
    
            /*
    -------------------------------------DROP DOWN MENU FOR AGE------------------------------------------
             */
            Spinner spinnerage=findViewById(R.id.spinner_age);
    
            ArrayAdapter<CharSequence>adapter4=ArrayAdapter.createFromResource(this, R.array.age, android.R.layout.simple_spinner_item);
            adapter4.setDropDownViewResource(android.R.layout.simple_spinner_item);
            spinnerage.setAdapter(adapter4);
    
            /*
    -------------------------------------DROP DOWN MENU FOR  STRENGTH------------------------------------------
             */
    
            Spinner spinnerstrength=findViewById(R.id.spinner_strength);
    
            ArrayAdapter<CharSequence>adapter1=ArrayAdapter.createFromResource(this, R.array.strength, android.R.layout.simple_spinner_item);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
            spinnerstrength.setAdapter(adapter1);
    
            /*
    -------------------------------------DROP DOWN MENU FOR  ENDURANCE------------------------------------------
             */
    
            Spinner spinnerendurance=findViewById(R.id.spinner_endurance);
    
            ArrayAdapter<CharSequence>adapter2=ArrayAdapter.createFromResource(this, R.array.endurance, android.R.layout.simple_spinner_item);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_item);
            spinnerendurance.setAdapter(adapter2);
    
            /*
    -------------------------------------DROP DOWN MENU FOR  CARDIO------------------------------------------
             */
    
            Spinner spinnercardio=findViewById(R.id.spinner_cardio);
    
            ArrayAdapter<CharSequence>adapter3=ArrayAdapter.createFromResource(this, R.array.cardio, android.R.layout.simple_spinner_item);
            adapter3.setDropDownViewResource(android.R.layout.simple_spinner_item);
            spinnercardio.setAdapter((adapter3));
    
        }
    
    }

--- XML activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".RegisterActivity">

    <!-- Text View to display our basic heading of "Registration" -->

    <TextView
        android:id="@ id/Registertitle"
        android:layout_width="194dp"
        android:layout_height="43dp"
        android:layout_marginStart="114dp"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="103dp"
        android:layout_marginBottom="502dp"
        android:scrollbarSize="30dp"
        android:text=" Registration "
        android:textSize="30dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <!--Edit Text for Jersey Number-->

    <EditText
        android:id="@ id/EditTextJerseyNumber"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="172dp"
        android:hint="Jersey Number"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="@ id/EditTextHeightNumber"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Edit Text for Weight Number-->

    <EditText
        android:id="@ id/EditTextWeightNumber"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="100dp"
        android:hint="Enter Weight"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="@ id/EditTextHeightNumber"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Edit Text for Height Number-->

    <EditText
        android:id="@ id/EditTextHeightNumber"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:hint="Enter Height"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Spinner is a drop down menu. This spinner is age selection -->

    <Spinner
        android:id="@ id/spinner_age"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="248dp"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="@ id/EditTextJerseyNumber"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Spinner is a drop down menu. This spinner is for gender selection-->


    <Spinner
        android:id="@ id/spinner_gender"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="304dp"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="@ id/spinner_age"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Spinner is a drop down menu. This spinner is strength -->

    <Spinner
        android:id="@ id/spinner_strength"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="360dp"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="@ id/spinner_gender"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Spinner is a drop down menu. This spinner is endurance -->

    <Spinner
        android:id="@ id/spinner_endurance"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="424dp"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="@ id/spinner_strength"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Spinner is a drop down menu. This spinner is cardio -->

    <Spinner
        android:id="@ id/spinner_cardio"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="488dp"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="@ id/spinner_endurance"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />

    <!--Button for verify to move to List Activity-->

    <Button
        android:id="@ id/verify"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="552dp"
        android:onClick="LaunchRtoL"
        android:text="Verify"
        app:layout_constraintEnd_toEndOf="@ id/spinner_cardio"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/Registertitle" />


</androidx.constraintlayout.widget.ConstraintLayout>

---Java ListsActivity.Java

package com.example.fitnessassessmentcell;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;


public class ListsActivity extends AppCompatActivity {

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

    }
}

CodePudding user response:

As per my understanding, You are trying to get selected data from the spinner and send it to another activity.

You have to add setOnItemSelectedListener for all the spinners and store selected values in a variable then you can send those values to another activity by passing those in intent. But you have to validate the values before sending those to another activity.

Let me know still you have any confusion...

CodePudding user response:

First retrieve your inputs (i.e) age, height, weight and so on, and store them in string for each input.

Then pass them on in the intent by using putExtra() method.

In RegisterActivity.java

Intent d = new Intent(RegisterActivity.this, ListsActivity.class);
d.putExtra("AGE", age);
d.putExtra("WEIGHT", weight);
d.putExtra("HEIGHT", height);
.
.
.
startActivity(d);

now you can get those values in ListActivity.java in the onCreate method

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

        String age = getIntent().getStringExtra("AGE");
        String weight = getIntent().getStringExtra("WEIGHT");
        String height = getIntent().getStringExtra("HEIGHT");
        .
        .
        .

    }

Hope this helps!!!

  • Related