[英]Android Spinner fill with object
How can I fill a spinner with an Object. 如何用对象填充微调框。
Example: I have User object userId, userName, firstName, lastName. 示例:我有用户对象userId,userName,firstName,lastName。 I need to fill display spinner with firstName.
我需要用firstName填充显示微调框。 If user select I need to get userId.
如果用户选择,我需要获取userId。
private void fillUserSpinner() {
spinnerUserList = findViewById(R.id.spinnerUserList);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, userDbHelp.gteUserIds());
spinnerUserList.setAdapter(arrayAdapter);
}
private void onUserSelected() {
spinnerUserList = findViewById(R.id.spinnerUserList);
spinnerUserList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//Need to get selected userId
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
You want display firstname alone, add this code into your model class: 您只想显示名字,请将以下代码添加到模型类中:
@Override
public String toString() {
return firstName;
}
use AppCompatSpinner together with an ArrayAdapter ... on case the items (POJO) of that ArrayAdapter
override method toString()
, one can skip manual assignment; 使用AppCompatSpinner与一起ArrayAdapter ...上的情况下的项目(POJO)
ArrayAdapter
重写方法toString()
可以跳过手动分配; that text is being displayed for the spinner's items then. 然后将显示微调框项目的文本。
@Override
public String toString() {
return this.firstName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.