如何解决所选项目上的 Android Spinner 不起作用?
所以伙计们,我正在尝试对我的微调器实施 OnItemSelectedListener。 数据从 firebase 中检索并添加到 arraylist 和我添加到适配器的数组列表
//spinner stufff
//variables:
Spinner schoolNamesspinner;
DatabaseReference databaseReference;
ArrayList<String> instituteNames;
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<>(studentsLogIn.this,android.R.layout.simple_spinner_dropdown_item,instituteNames);
schoolNamesspinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
**// populating selector;**
populatingSelecto();
填充微调器 enter image description here
**// populating selector**
private void populatingSelecto() {
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
for (DataSnapshot ids : snapshot.getChildren()) {
uniqIds.add(ids.getKey());
}
for (int i = 0; i < uniqIds.size(); i++) {
databaseReference.child(uniqIds.get(i)).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
String name = snapshot.child("INSTITUTE").child("institutionName").
getValue().toString();
instituteNames.add(name);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
} else {
dialogBoxMeathod(studentsLogIn.this,"No Institutes on the Record");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
因此,在选定的项目上,我试图显示选定项目的文本,但它不起作用
schoolNamesspinner.setonItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
Toast.makeText(studentsLogIn.this,instituteNames.get(position),Toast.LENGTH_SHORT).show();
}
@Override
public void onnothingSelected(AdapterView<?> parent) {
}
});
解决方法
我必须在添加内容后立即通知适配器
for (int i = 0; i < uniqIds.size(); i++) {
databaseReference.child(uniqIds.get(i)).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
String name = snapshot.child("INSTITUTE").child("institutionName").getValue().toString();
instituteNames.add(name);
// here i have to add the adapter.notifydatachanged
}
}
}
}
,
您尚未初始化微调器:
Spinner schoolNamesSpinner;
DatabaseReference databaseReference;
ArrayList<String> instituteNames;
ArrayAdapter<String> adapter;
schoolNamesSpinner=findViewById(R.id.your_spinner_id);//this line
adapter = new ArrayAdapter<>(studentsLogIn.this,android.R.layout.simple_spinner_dropdown_item,instituteNames);
schoolNamesSpinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
**// populating selector;**
populatingSelecto();
schoolNamesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
Toast.makeText(studentsLogIn.this,instituteNames.getItemAtPosition(position),Toast.LENGTH_SHORT).show();//use getItemAtPosition
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。