我正在从Firebase接收数据,并且Spinner中的数据加载正常,但是当我选择任何项目时onItemSelectListener不起作用,或者我们可以说项目没有从Spinner中获得选择
public class Center_changes extends AppCompatActivity {
ArrayList<String> array_list=new ArrayList<>();
EditText center_name,center_url;
Button save_btn,delete_btn;
Spinner centers_spn;
ArrayList<String> arrayList2=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_center_changes);
center_name=(EditText)findViewById(R.id.center_name);
center_url=(EditText)findViewById(R.id.center_url);
save_btn=(Button)findViewById(R.id.save_btn);
delete_btn=(Button)findViewById(R.id.delete_btn);
centers_spn=(Spinner)findViewById(R.id.center_spinner_1);
centers_spn.setSelected(true);
FirebaseDatabase database=FirebaseDatabase.getInstance();
DatabaseReference data_ref=FirebaseDatabase.getInstance().getReference();
Query arry_q=data_ref.child("centers").orderByChild("center_name");
arry_q.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren())
{
array_list.clear();
for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
{
data dat=dataSnapshot1.getValue(data.class);
array_list.add(dat.getCenter_name());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
ArrayAdapter<String> adapter=new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,array_list);
//adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
centers_spn.setAdapter(adapter);
centers_spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// ((TextView)parent.getChildAt(position)).setTextSize(25);
// ((TextView)parent.getChildAt(position)).setTextColor(Color.RED);
Toast.makeText(Center_changes.this,"Item="+parent.getSelectedItem().toString()+" and id="+parent.getSelectedItemId(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}