0

I have some problems related to my database, and the docs seems a lot of confusing i can't figure out how to solve that, basicly i have my user data in the firebase, and i want to see if in my user with some id i already have the key username if yes i want to update with some data of my form, if no i want to add a value based on my form, but when i want to know how to check if the id already exist, i did this:

  auth = FirebaseAuth.getInstance();
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference();
        userId = auth.getCurrentUser().getUid();
        user =  myRef.child("Users").child(userId);
        Log.d("test1",String.valueOf(user.child("age").getKey()));
        if(user.child("username").co){
            usernameTxt.setText(user.child("username").toString());
        }
        if(user.child("age") != -1){
            usernameTxt.setText(user.child("age").toString());
        }

data

1 Answers1

2

Try this

final FirebaseAuth auth = FirebaseAuth.getInstance();
    DatabaseReference mDatabase=FirebaseDatabase.getInstance().getReference().child("Users").child(auth.getCurrentUser().getUid());
    mdatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if(dataSnapshot.hasChild("username")){
                //IT EXISTS
            }
            else{
                //IT DOESNT EXISTS
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
Martin De Simone
  • 2,108
  • 3
  • 16
  • 30
  • i feel i almost did it, but how can i retrive a single value of snapshot? i did this: dataSnapshot.child("username").getValue(), inspected that with logcat and it gives me this: https://friendlymanager-3b7a2.firebaseio.com/Users/g7LWSXBP2WYwBbDqPS82oEpILA22/username :S –  Apr 12 '17 at 15:08
  • You have to cast it use getValue(String.class); – Martin De Simone Apr 12 '17 at 16:01
  • Have you tested my answer? – Martin De Simone Apr 12 '17 at 16:03
  • yeah i did it, i will accept the answer, a last question, i have a User model in a separate file and i save the data using it, i saw some examples where they use the dataSnapshot with the model, in this case can i do it to do a better approach?´ –  Apr 12 '17 at 16:15
  • Do you want to get the data or write it? – Martin De Simone Apr 12 '17 at 17:19
  • i want to get it to display on my activity, and then when i press update data, add or update the data on firebase, but in this example i just want to get –  Apr 12 '17 at 17:24
  • Use getValue (Yourmodel.class) in the dataSnaphot – Martin De Simone Apr 12 '17 at 17:39
  • can i add you to chat ? –  Apr 12 '17 at 17:45
  • Yes, i dont have any problem – Martin De Simone Apr 12 '17 at 18:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141583/discussion-between-filipe-costa-and-martin-de-simone). –  Apr 12 '17 at 19:20