コードは次のとおりです。どのようにオブジェクトをアンドロイドのスピナーに渡すことができますか?
public class Organization {
private String name;
private Long id;
public Organization(){
}
public Organization(String name, Long id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
スピナーは次のとおりです。
Spinner sp = (Spinner) navigationView.getMenu().findItem(R.id.brand_spinner).getActionView();
sp.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,contactList));
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String value = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),""+value+"",Toast.LENGTH_SHORT).show();
}
});
で名前を選択することで、私はスピナーに両方の名前とidを渡すことができますし、スピナーにリストする必要がありますどのようにスピナー。名前のidをローカル変数に格納します。
http://stackoverflow.com/questions/1625249/android-how-to-bind-spinner-to-custom-object-list –
カスタムアダプタが必要です
このため。 –
http://stackoverflow.com/questions/35983176/how-to-create-spinner-list-using-customadapter-in-android –