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

时间:2016-01-14 20:20:54

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

我有这个对象的ArrayList,我需要将它设置为我的spinner的适配器,如下所示:

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

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

我需要让微调器显示来自ArrayList的联系人姓名,因为它显示了内存中的联系地址,选中后,我需要返回联系人ID,以执行其他操作。 我怎么能这样做?

2 个答案:

答案 0 :(得分:62)

您需要做的很容易,对您的班级联系,覆盖其中的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;
        }
    }

}
  

输出

contact_image

答案 1 :(得分:0)

一个更强大的解决方案可以是获取需要显示的内容的唯一值,然后再制作一个列表。

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