Android旋转器上选择的项目不工作?



所以伙计们,我正试图实现OnItemSelectedListener敬我的陀螺。从firebase检索数据并将其添加到数组列表中,然后将该数组列表添加到适配器

//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();

填充旋转控件输入图片描述

**// 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) {
}

相关内容

  • 没有找到相关文章