使用对象数组列表作为微调适配器 [英] Use object array list as spinner adapter

查看:67
本文介绍了使用对象数组列表作为微调适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个对象的ArrayList,需要将其设置为微调器的适配器,如下所示:

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

这是我的Contact对象的一个​​示例,它只有两个变量,即Name和ID

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")

我需要使微调框显示ArrayList中的联系人的名称,因为它在内存中显示了联系人地址,并且在选择后,我需要返回联系人ID,以执行其他操作. 我该怎么办?

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?

推荐答案

您需要做的很简单,对于您的类Contact,重写其中的toString()方法并返回联系人的名称.

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.

看例子.也可以在中获得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;
        }
    }

}

输出

这篇关于使用对象数组列表作为微调适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆