如何为微调框填充对象。

示例:我有用户对象userId,userName,firstName,lastName。我需要用firstName填充显示微调框。如果用户选择,我需要获取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) {

                }
            });
        }

最佳答案

您只想显示名字,请将以下代码添加到模型类中:

@Override
public String toString() {
    return firstName;
}

07-27 18:02