Use object array list as spinner adapter

Rafael picture Rafael · Jan 14, 2016 · Viewed 64.1k times · Source

I got this ArrayList of objects, and i need to set it as my spinner's adapter like this:

ArrayList<Contact> contactlist= new ArrayList<Contact>();
contactlist.add("Gabe");
contactlist.add("Mark");
contactlist.add("Bill");
contactlist.add("Steve");

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, contactlist);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

contactsSpinner.setAdapter(adapter);

This is a example of my Contact object, it only have two variables, Name and ID

Contact contact = new Contact();
    contact.setName("Gabe")
    contact.setID("14575")

I need to make the spinner show the name of the contact from the ArrayList because it's showing the contact address in the memory, and when selected, I need to return the contact ID, to perform another operation. How can I do this?

Answer

Pankaj Nimgade picture Pankaj Nimgade · Jan 15, 2016

Hi what you need to do is pretty easy, to your class Contact, override the toString() method in it and return the name of the contact.

look at the example. it is also available in github

public class SpinnerTestOneActivity extends AppCompatActivity {

    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_test_one);
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_custom_toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initializeUI();
    }

    private void initializeUI() {

        spinner = (Spinner) findViewById(R.id.SpinnerTestOneActivity_spinner);

        ArrayList<Contact> contacts = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            contacts.add(new Contact("Name_" + i, "Id_" + i));
        }

        ArrayAdapter<Contact> adapter =
                new ArrayAdapter<Contact>(getApplicationContext(),  android.R.layout.simple_spinner_dropdown_item, contacts);
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);

    }

    private class Contact {
        private String contact_name;
        private String contact_id;

        public Contact() {
        }

        public Contact(String contact_name, String contact_id) {
            this.contact_name = contact_name;
            this.contact_id = contact_id;
        }

        public String getContact_name() {
            return contact_name;
        }

        public void setContact_name(String contact_name) {
            this.contact_name = contact_name;
        }

        public String getContact_id() {
            return contact_id;
        }

        public void setContact_id(String contact_id) {
            this.contact_id = contact_id;
        }

        /**
         * Pay attention here, you have to override the toString method as the
         * ArrayAdapter will reads the toString of the given object for the name
         *
         * @return contact_name
         */
        @Override
        public String toString() {
            return contact_name;
        }
    }

}

output

contact_image