Android 在 Spinner 中选择项目时如何从 ROOM 数据库获取特定数据
How to get specific data from ROOM database when an item is selected in Spinner in Android
我有一个学生实体,它包含学生姓名、学生手机号码。我正在使用学生姓名添加学生记录,我在添加新记录时从微调器中 selecting 学生姓名。所以我的问题是,当我 select 学生姓名时,我想在微调器下方的文本视图中获取该特定学生的手机号码。
这就是我在微调器中获取所有名称的方式
private void loadSpinnerData() {
studentViewModel = ViewModelProviders.of(this).get(StudentViewModel.class);
studentViewModel.getAllStudents().observe(this, new Observer<List<Student>>() {
@Override
public void onChanged(List<Student> students) {
String[] nameLists = new String[students.size()];
for (i = 0; i < students.size(); i++) {
nameLists[i] = students.get(i).getStudentName() + " " + students.get(i).getStudentFatherName() +
" " + students.get(i).getStudentSurname();
}
ArrayAdapter<String> studentNames = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, nameLists);
studentNames.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectStudentName.setAdapter(studentNames);
}
});
}
这是我的旋转适配器
selectStudentName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> adapterView, View view, final int position, long l) {
if ( position == -1 ) {
Toast.makeText(AddRecord.this, "No record is selected", Toast.LENGTH_SHORT).show();
} else {
String name = adapterView.getItemAtPosition(position).toString();
Toast.makeText(AddRecord.this, name + " is selected", Toast.LENGTH_SHORT).show();
for (int i = 0; i < adapterView.getCount(); i++ ){
}
}
}
如何获得 selected 学生姓名的手机号码?谢谢!
简单亲爱的,只需使用您的视图模型 class object(studentViewModel) 来获取像这样的实时数据实例-
selectStudentName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> adapterView, View view, final int position, long l) {
if ( position == -1 ) {
Toast.makeText(AddRecord.this, "No record is selected", Toast.LENGTH_SHORT).show();
} else {
String studentMobileNumber = studentViewModel.getAllStudents().getValue().get(position).getMobileNumber()
}
}
我有一个学生实体,它包含学生姓名、学生手机号码。我正在使用学生姓名添加学生记录,我在添加新记录时从微调器中 selecting 学生姓名。所以我的问题是,当我 select 学生姓名时,我想在微调器下方的文本视图中获取该特定学生的手机号码。
这就是我在微调器中获取所有名称的方式
private void loadSpinnerData() {
studentViewModel = ViewModelProviders.of(this).get(StudentViewModel.class);
studentViewModel.getAllStudents().observe(this, new Observer<List<Student>>() {
@Override
public void onChanged(List<Student> students) {
String[] nameLists = new String[students.size()];
for (i = 0; i < students.size(); i++) {
nameLists[i] = students.get(i).getStudentName() + " " + students.get(i).getStudentFatherName() +
" " + students.get(i).getStudentSurname();
}
ArrayAdapter<String> studentNames = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, nameLists);
studentNames.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectStudentName.setAdapter(studentNames);
}
});
}
这是我的旋转适配器
selectStudentName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> adapterView, View view, final int position, long l) {
if ( position == -1 ) {
Toast.makeText(AddRecord.this, "No record is selected", Toast.LENGTH_SHORT).show();
} else {
String name = adapterView.getItemAtPosition(position).toString();
Toast.makeText(AddRecord.this, name + " is selected", Toast.LENGTH_SHORT).show();
for (int i = 0; i < adapterView.getCount(); i++ ){
}
}
}
如何获得 selected 学生姓名的手机号码?谢谢!
简单亲爱的,只需使用您的视图模型 class object(studentViewModel) 来获取像这样的实时数据实例-
selectStudentName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> adapterView, View view, final int position, long l) {
if ( position == -1 ) {
Toast.makeText(AddRecord.this, "No record is selected", Toast.LENGTH_SHORT).show();
} else {
String studentMobileNumber = studentViewModel.getAllStudents().getValue().get(position).getMobileNumber()
}
}