我创建了一个数据库表 student,其中包含两个字段,如 student_id 和 student_name。此表包含学生的一些数据。
My actual problem is I want to set ID and name of student in spinner like in html select tag, so when select spinner item I must get this ID to get another data from database.
最佳答案
name 和 id 可以有两个数组列表;
List<String> sIds = new ArrayList<String>();//add ids in this list
List<String> names = new ArrayList<String>();//add names in this list
然后您可以在微调器选择中在此处检索。即使微调器中的两个名称相同,它也会起作用,可能是这种情况:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int id = sIds.get(position);//This will be the student id.
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
关于java - 如何在微调器中设置 ID 和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717003/