我的数据来自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();
}
答案 0 :(得分:1)
您必须在datachange之后设置适配器,如下所示:
//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);
答案 1 :(得分:0)
ArrayList<String> issueCategoryArrayList = new ArrayList<String>();
FirebaseDatabase.getInstance().getReference("issue_categories").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
issueCategoryArrayList.add(dataSnapshot1.getValue(IssueCategory.class).getTitle());
}
spinner = findViewById(R.id.post_category);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(PostActivity.this,
android.R.layout.simple_spinner_item, issueCategoryArrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});