android - 使用对象数组列表作为微调器适配器

标签 android user-interface drop-down-menu spinner

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

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

这是我的联系人对象的示例,它只有两个变量,名称和 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. 我该怎么做?

最佳答案

您好,您需要做的很简单,对于您的联系人类,覆盖其中的 toString() 方法并返回联系人的姓名。

看例子。它也可以在 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

关于android - 使用对象数组列表作为微调器适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34798967/

相关文章:

android - 从运行 Oreo 且没有 REQUEST_INSTALL_PACKAGES 的 android 应用程序安装 apk

java - 如何在 Eclipse 中更改主题(整个主题,而不是字体!)

javascript - 根据第一个下拉选择 jquery 显示第二个下拉选项

css - Bootstrap : Position of dropdown menu relative to navbar item

onChange 事件上的 Javascript href 更改

android - 解析数据时出错 org.json.JSONException : Value <? java.lang.String 类型的 xml 无法转换为 JSONArray

Android 6.0.1 读写外置SD卡

android列表指向位置

winapi - SetWindowText 慢,Win32 C++

user-interface - 如何在 Julia 中制作 GUI?