Android Simple Spinner – Fill with List Of Objects

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.

===

Demo

Here is the demo of this project.

First things first, let’s start by some introductions.

API Reference – Important APIs to Know First

Here are the API’s we’ve used for this project.

2. How to Import Spinner

First we imported Spinner from it’s android.widget pakage:

import android.widget.Spinner;

3. How to Reference spinner from Layout

Then referenced using the findViewById() of the Activity class:

Spinner sp= (Spinner) findViewById(R.id.sp); 
(b). List – an Interface

2. How to use a java List

We use it and it’s implementer the ArrayList to hold our list of person Objects.

We’ve instantiated it and passed

        List<String> people=new ArrayList<>(); 

Then cleared it:

            people.clear();

and used the add() method to add Person objects:

            Person p=new Person(); 
            p.setName("Mike"); 
            people.add(p.getName()); 
            p=new Person(); 
            p.setName("John"); 
           ....
(c). ArrayAdapter – an Adapter

We will use an ArrayAdapter instance to bind our data to our spinner.

2. How to Instantiate an ArrayAdapter

You pass:

  1. The Context
  2. The Resource
  3. The List
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,people); 

3. How to Bind Set an ArrayAdapter to a Spinner

You use the setAdapter() method.

        sp.setAdapter(adapter); 

Full Code

Let’s now look at the full source code.

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

Our main activity class.

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());
            }
}

Resoources.

Android platform provides a powerful and flexible way of adding static content as a resource.

These static content will also be packaged into the APK file. The static content will be stored either as a resource or as an asset.

Resources belong to a given type. These types can be:

  1. Drawable.
  2. Layout.
  3. Value.

Let’s start by looking at the layout resources

(a). activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    
    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 : Source Code Reference

GitHub : Source

Best Regards,

Oclemy.

Share



Share an Example

Share an Example

Browse
What is the capital of Egypt? ( Cairo )