Ответ 1
Привет, что вам нужно сделать, довольно легко, для вашего класса Contact, переопределить метод 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;
}
}
}
вывод