我是Firebase的新手。我想将Firebase数据添加到我的微调器中。我按照这个答案How to get a String List from Firebase to fill a Spinner但我的锭床工作者没有收到任何答案。
这是我的代码:
//Value event listener for realtime data update
ref.child("Tabela-Consulta").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
final List<String> nomeConsulta = new ArrayList<String>();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
TabelaConsulta consultaName = postSnapshot.child("nomeconsulta").getValue(TabelaConsulta.class);
nomeConsulta.add(consultaName.getConsultanome());
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, nomeConsulta);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
我的数据模型:
public class TabelaConsulta {
private String consultanome;
public TabelaConsulta(){
}
public String getConsultanome() {
return consultanome;
}
public void setConsultanome(String consultanome){
this.consultanome = consultanome;
}
}
由于
答案 0 :(得分:0)
试试这个:
myRef.child("Tabela-Consulta").child("nomeconsulta").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> nomeConsulta = new ArrayList<String>();
for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
String consultaName = areaSnapshot.getValue(String.class);
nomeConsulta.add(consultaName);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(YOUR_ACTIVITY.this, android.R.layout.simple_spinner_item, nomeConsulta);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(skularAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
请记住定义您的firebase参考:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("YOUR_REFERENCE");
定义你的微调器:
Spinner spinner = (Spinner) findViewById(YOUR_SPINNER);
还要使您的数据库看起来像这样:
通过添加您的值:
将您的值放在" "
中,然后使用[]
关闭所有内容。
希望它有所帮助!
答案 1 :(得分:-1)
请按照以下
Firebase网址:https://example-id:firebaseio.com/Tabela-Consulta
MainActivity.java
:
public class MainActivity extends Activity {
private List<String> nomeConsulta = new ArrayList<String>();
private ArrayAdapter<String> dataAdapter;
private DatabaseReference mDatabaseReference;
private Spinner mSpinner
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, nomeConsulta);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner = (Spinner)findViewById(R.id.your_reference_spinner);
mSpinner.setAdapter(dataAdapter);
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.child("Tabela-Consulta").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: snapshot.getChildren()) {
TabelaConsulta data = snapshot.getValue(TabelaConsulta.class);
nomeConsulta.add(data.getConsultanome());
}
dataAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}