activity_main.xmlUser.javaMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:gravity="center" tools:context="com.codinginflow.spinnercustomobjectexample.MainActivity"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getSelectedUser" android:text="Get selected User" /> </LinearLayout>
package com.codinginflow.spinnercustomobjectexample; public class User { private String name; private int age; private String mail; public User(String name, int age, String mail) { this.name = name; this.age = age; this.mail = mail; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } @Override public String toString() { return name; } }
package com.codinginflow.spinnercustomobjectexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = findViewById(R.id.spinner); List<User> userList = new ArrayList<>(); User user1 = new User("Jim", 20, "jim@gmail.com"); userList.add(user1); User user2 = new User("John", 23, "john@gmail.com"); userList.add(user2); User user3 = new User("Jenny", 25, "jenny@gmail.com"); userList.add(user3); ArrayAdapter<User> adapter = new ArrayAdapter<User>(this, android.R.layout.simple_spinner_item, userList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { User user = (User) parent.getSelectedItem(); displayUserData(user); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } public void getSelectedUser(View v) { User user = (User) spinner.getSelectedItem(); displayUserData(user); } private void displayUserData(User user) { String name = user.getName(); int age = user.getAge(); String mail = user.getMail(); String userData = "Name: " + name + "\nAge: " + age + "\nMail: " + mail; Toast.makeText(this, userData, Toast.LENGTH_LONG).show(); } }