35

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?

2
  • It's not clear to me what your question is exactly, or even how many questions you're asking. Can you explain what the code is doing that's incorrect, or exactly what you need it to do that it isn't doing?
    – nasch
    Jan 15, 2016 at 0:01

3 Answers 3

119

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

0
0

One more robust solution can be taking the only value of what needs to be displayed and made one more list.

private ArrayAdapter adapter;
private List<CategoryHelper> categoryList = STORE.getCategoryList();
private List<String> names=new ArrayList<>();

adapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_spinner_item, names);
        adapter.setDropDownViewResource(android.R.layout
                .simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

   for(CategoryHelper helper:categoryList){
          names.add(helper.getName());
       }
       
       adapter.notifyDataSetChanged();
0

in kotlin val adapter02: ArrayAdapter = ArrayAdapter( applicationContext, android.R.layout.simple_spinner_dropdown_item, sArray ) adapter02.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

   adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
   spinner.adapter = adapter02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.