我的数据来自Firebase数据库。我成功地将busNum填充到spinner。但是我的问题是,每当我要选择busnum时,我的spinner都不会显示任何内容。这是一些代码。
List<String> busnum = new ArrayList<String>();

busRef =  new Firebase(Config.FIREBASE_URL_BUSNUMBER);
    busRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                busnum.add(postSnapshot.child("busNum").getValue().toString());
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Toast.makeText(AddDriverActivity.this, "Failed to read data.... " +firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, busnum);
    mSpinBusNum.setPrompt("Select bus number");
    mSpinBusNum.setAdapter(adapter);

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    busNum = parent.getItemAtPosition(position).toString();
}

最佳答案

您必须在数据更改后设置适配器,如下所示:

//after onDataChange
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, busnum);
mSpinBusNum.setPrompt("Select bus number");
// Apply the adapter to the spinner
mSpinBusNum.setAdapter(adapter);

09-12 21:58