本文介绍了在firebase android中获取特定值的推送ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索一下firebase生成的id,当我像下一个值一样推送它的时候



我想检索-KGdKip ....这个电子邮件$ id $ b我试过getKey(),但它返回用户
,当用户得到值它返回整个对象从ID到配置文件picuter,并不会让我把它作为用户对象在我的应用程序

如何解决这个问题?

  Firebase用户= myFirebaseRef.child(users); 
users.orderByChild(email)。equalTo([email protected])。addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){

dataSnapshot.getKey();
Log.d(User,dataSnapshot.getRef()。toString());
Log.d(User,dataSnapshot。 getValue()。toString());
}

@Override
public void onCancelled(FirebaseError firebaseError){

Log.d ,firebaseError.getMessage());
}
});


解决方案

UPDATE 1:
一行
$ b

// *********** ************************************************** * //



在搜索并尝试了很多事情后,我来到了两种方法来做

1.第一个获取密钥,当我通过这个函数上传到服务器的帖子

  public void uploadPostToFirebase (Post post){
DatabaseReference mFirebase = mFirebaseObject
.getReference(Constants.ACTIVE_POSTS_KEY)
.child(post.type);
mFirebase.push()。setValue(post);
Log.d(Post Key,mFirebase.getKey());

$ / code>




  1. 在我已经把它推到我的数据库中的节点之后得到这个键值。 b
    $ b查询queryRef = databaseRef.child(Constants.USERS_KEY)
    .orderByChild(Constants.USERS_EMAIL)
    .equalTo(email);

    queryRef.addChildEventListener(new ChildEventListener(){
    @Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
    // TODO auto generated


    @Override
    public void onChildChanged(DataSnapshot dataSnapshot,String s){
    // TODO auto generated;
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot){
    // TODO auto generated;
    }
    $ b $ @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s){
    // TODO auto generated
    }

    @Override
    public void onCancelled(DatabaseError databaseError){
    // TODO auto generated
    }
    });



I want to retrive the id that generated by firebase when I pushed value to it like next

I want to retrieve "-KGdKip...." this id for that email I tried by getKey() but it return "users"and when user get value it return the whole object from the id to profile picuter and that won't make me get it as User object in my app

how solve this ?

    Firebase users = myFirebaseRef.child("users");
    users.orderByChild("email").equalTo("[email protected]").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            dataSnapshot.getKey();
            Log.d("User",dataSnapshot.getRef().toString());
            Log.d("User",dataSnapshot.getValue().toString());
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

            Log.d("User",firebaseError.getMessage() );
        }
    });
解决方案

UPDATE 1: it can obtain also by one line

//**************************************************************//

after searching and trying a lot of things i came to 2 ways to do that . 1. first one to get the key when i upload the post to the server via this function

 public void uploadPostToFirebase(Post post) {
      DatabaseReference mFirebase = mFirebaseObject
            .getReference(Constants.ACTIVE_POSTS_KEY)
            .child(post.type);
      mFirebase.push().setValue(post);
      Log.d("Post Key" , mFirebase.getKey());
 }
  1. i used it in my code to get the key after i have already pushed it to node for it in my database

    public void getUserKey(String email) {
    
        Query queryRef = databaseRef.child(Constants.USERS_KEY)
            .orderByChild(Constants.USERS_EMAIL)
            .equalTo(email);
    
        queryRef.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated
          }
    
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated;
          }
    
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
            //TODO auto generated;
          }
    
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated
          }
    
          @Override
          public void onCancelled(DatabaseError databaseError) {
            //TODO auto generated 
          }
      });
     }
    

这篇关于在firebase android中获取特定值的推送ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:02