问题
I want to get a string from this:
, but it has a unique key parent. How do I get string from db? I tried:
firebaseAuth = FirebaseAuth.getInstance();
fUID =firebaseAuth.getCurrentUser().getUid();
itemsUrl ="https://nextweaverproject.firebaseio.com/users/" + fUID ;
mDatabase = FirebaseDatabase.getInstance();
final DatabaseReference myRef = mDatabase.getReferenceFromUrl(itemsUrl);
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue();
List<Object> values = new ArrayList<Object>(td.values());
strFb = new ArrayList<String>();
strFb.add(values.get(0).toString());
// strFb.add(urlLong);
Log.v("test"," " + strFb.get(strFb.size()-1));
}
But It returns all objects in db.
回答1:
You can use child to get data from the database tree:
myRef.child("Scenes").child("Scene").("******").
(*****).addChildEventListener(new ChildEventListener() {
this way you should chain your fields.
You can use also addValueEventListener, addListenerForSingleValueEvent.
You can read about them here:
https://firebase.google.com/docs/database/android/retrieve-data
回答2:
Thank you all. I found my solution by this:
myRef.child("Scenes").child("Scene").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot children : dataSnapshot.getChildren()) {
for (DataSnapshot child : children.getChildren()) {
//Log.v("key1"," " + child.getKey());
if(child.getKey().equals("Thumb")){
for (DataSnapshot child2 : child.getChildren()) {
//Log.v("key2"," " + child2.getValue(String.class));
for (DataSnapshot child3 : child2.getChildren()) {
//Log.v("key3"," " + child3.getKey());
if(child3.getKey().equals("LongUrl")){
Log.v("key4"," " + child3.getValue(String.class));
thumbUrl.add(child3.getValue().toString());
}
}
}
}
}
Log.v("keyResult"," " + thumbUrl);
// Log.v("key2"," " + thumbUrl);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Result here :Result
来源:https://stackoverflow.com/questions/37919455/how-to-get-string-from-firebase-realtime-db-that-contain-in-unique-key