I am making an activity with 3 spinners, First spinner only have one value for now but second spinner have total of six values. I want populate 3rd spinner on base of items selected on second spinner (because first spinner only have 1 item). Can any one explain how can I do it?
I thought about doing it with switch case but I don’t know how I can store spinner values in variable. I also searched for it on google and found onItemSeleted() method but I cant understand how to use it.
XML file :
<?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=".ENOTES">
<TextView
android:id="@+id/Department_text"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="108dp"
android:text="Department"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/department_text2"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="80dp"
android:text="Subject"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sem_spinner" />
<TextView
android:id="@+id/Sem_text"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="80dp"
android:text="Semester"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dept_spinner" />
<Spinner
android:id="@+id/dept_spinner"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Department_text" />
<Spinner
android:id="@+id/sem_spinner"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="28dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Sem_text" />
<Spinner
android:id="@+id/subject_spinner"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="28dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/department_text2" />
<Button
android:id="@+id/get_notes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.888"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
</androidx.constraintlayout.widget.ConstraintLayout>
java :
package com.example.project;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import java.util.List;
public class ENOTES extends AppCompatActivity
{
Spinner dept_spinner, sem_spinner, subject_spinner;
Button get_notes_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enotes);
//Variable Initializations
dept_spinner = findViewById(R.id.dept_spinner);
sem_spinner = findViewById(R.id.sem_spinner);
subject_spinner = findViewById(R.id.subject_spinner);
get_notes_button = findViewById(R.id.get_notes_button);
//Setting dept_spinner adapter
ArrayAdapter<CharSequence> ad_dept = ArrayAdapter.createFromResource(this,
R.array.department_names, android.R.layout.simple_spinner_item);
ad_dept.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dept_spinner.setAdapter(ad_dept);
//setting sem_spinner adapter
ArrayAdapter<CharSequence> ad_sem = ArrayAdapter.createFromResource(this,
R.array.sem_names, android.R.layout.simple_spinner_item);
ad_sem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sem_spinner.setAdapter(ad_sem);
}
}
Advertisement
Answer
User selected item listener to get selected option for first two spinner and then show data in third spinner according to that
String a;
String b;
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// assign data to 'a'
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// assign data to 'b'
setDatatoSpinner3(a,b)
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
function setDatatoSpinner3(String a, String b){
// your data logic here
}
Updated code according to your code
package com.example.project;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import java.util.List;
public class ENOTES extends AppCompatActivity
{
Spinner dept_spinner, sem_spinner, subject_spinner;
Button get_notes_button;
String dept_selection = "", sem_selection = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enotes);
//Variable Initializations
dept_spinner = findViewById(R.id.dept_spinner);
sem_spinner = findViewById(R.id.sem_spinner);
subject_spinner = findViewById(R.id.subject_spinner);
get_notes_button = findViewById(R.id.get_notes_button);
//Setting dept_spinner adapter
ArrayAdapter<CharSequence> ad_dept = ArrayAdapter.createFromResource(this,
R.array.department_names, android.R.layout.simple_spinner_item);
ad_dept.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dept_spinner.setAdapter(ad_dept);
//setting sem_spinner adapter
ArrayAdapter<CharSequence> ad_sem = ArrayAdapter.createFromResource(this,
R.array.sem_names, android.R.layout.simple_spinner_item);
ad_sem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sem_spinner.setAdapter(ad_sem);
dept_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
dept_selection = ad_dept.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
sem_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
sem_selection = ad_sem.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
function updateSubjects(String dept, String sem){
//setting sem_spinner adapter
if(dept.equals('CIVIL') && sem.equals('1st') ){
ArrayAdapter<CharSequence> ad_subj = ArrayAdapter.createFromResource(this,
R.array.civil_1st_sem, android.R.layout.simple_spinner_item);
} else if(dept.equals('CIVIL') && sem.equals('2nd') ){
ArrayAdapter<CharSequence> ad_subj = ArrayAdapter.createFromResource(this,
R.array.civil_2nd_sem, android.R.layout.simple_spinner_item);
}
ad_subj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subject_spinner.setAdapter(ad_subj);
}
}