问题
I have my realtime database as this. Users=>UID=>Medicines=>Date=>Medicine Name :Values
I have a spinner
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(arrayAdapter);
I tried this:
mUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Medical");
// GET ALL DATES // GET ALL MEDICINE NAME // GET ALL Values //
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.v("DATES : ",""+ dataSnapshot.getKey());
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
Log.v("Medicine Name",""+ childDataSnapshot.getKey());
arraList.add(childDataSnapshot.getKey());
Log.v("VALUES",""+ childDataSnapshot.child(childDataSnapshot.getKey()).getValue());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
BUT IT IS EMPTY AND I AM UNABLE TO READ ANY OF THEM.
回答1:
You have written Medical instead of "Medicines".
You can either delete or leave this as solved as rest looks correct
Also childSnapshot.getKey()
will give you DATES and childSnapshot.getValue()
will give you entire thing inside date
Also If I got you then you can try this:
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
//"DATES:"+dataSnapshot.getKey()
for (DataSnapshot childDataSnapshot2 : childDataSnapshot.getChildren()) {
// "MEDICINE NAME"+childDataSnapshot2.getKey()
arrayList.add(childDataSnapshot2.getKey());
//"VALUES"+childDataSnapshot2.getValue()
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
来源:https://stackoverflow.com/questions/59499322/i-want-to-list-all-unique-childrens-and-keys-along-with-values-in-a-spinner-fire