How to get string from firebase realtime db that contain in unique key

天大地大妈咪最大 提交于 2019-12-22 09:11:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!