Android Simple Spinner - Fill with List Of Objects

July 2, 2017 Oclemy Android Spinner, Java Generic List 2 minutes, 2 seconds

Hello guys.How do you do? This is what we do:

  1. Populate arraylist with Person objects.
  2. Pass this arraylist to our ArrayAdapter.
  3. Set our adapter to our spinner
  4. Handle ItemSelection events,hence show a toast.

Project Demo

SECTION 1 : Our Person Class

  • Is our data object.
  • Represents a single person with his properties.
package com.tutorials.hp.spinnerandobjects;

public class Person {

    int id;
    String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

SECTION 2 : Our MainActivity

package com.tutorials.hp.spinnerandobjects;
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 { 
    List<String> people=new ArrayList<>(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        //SPINNER 
        Spinner sp= (Spinner) findViewById(R.id.sp); 
        //FILL LIST 
        fillPeople(); //
        ADAPTER ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,people); sp.setAdapter(adapter); 
        //item selected 
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override 
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
            Toast.makeText(MainActivity.this, people.get(i), Toast.LENGTH_SHORT).show(); }
             @Override 
             public void onNothingSelected(AdapterView<?> adapterView) {

              } 
            }); 
        } 
        private void fillPeople() { 
            people.clear(); 
            Person p=new Person(); 
            p.setName("Mike"); 
            people.add(p.getName()); 
            p=new Person(); 
            p.setName("John"); 
            people.add(p.getName()); 
            p=new Person(); 
            p.setName("Lucy"); 
            people.add(p.getName()); 
            p=new Person(); 
            p.setName("Rebecca"); 
            people.add(p.getName()); 
            p=new Person(); 
            p.setName("Kris"); 
            people.add(p.getName()); 
            p=new Person(); 
            p.setName("Kurt"); 
            people.add(p.getName()); 
            p=new Person(); 
            p.setName("Vin"); 
            people.add(p.getName());
            }
}

SECTION 3 : Our Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tutorials.hp.simplespinner.MainActivity">

    <Spinner
        android:id="@+id/sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

SECTION 4 : Our Result

spinner objects

SECTION 4 : Source Code Reference

GitHub : Source

Comments