show and hide buttons using firebase reference

Viewed 395

show and hide buttons using firebase reference

Hello. I have a question I need to display the casino if I have the casino node. if you do not have the casino node that will display the buy casino button. I am using "if" "else" but apparently he is not checking the if because in any way for the user who has the casino node he displays the button "buycassino" and in the user who does not have the casino node he also displays the button "buycassino" I am attaching the debug along with the firebase structure. Thanks in advance. any help is welcome.

code:

   final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    final String userId = user.getUid();


    ref.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                if (ds.child("cassino").exists())  {
                    openCassino.setVisibility(View.VISIBLE);
                    buyCassino.setVisibility(View.GONE);
                } else {
                    buyCassino.setVisibility(View.VISIBLE);
                    openCassino.setVisibility(View.GONE);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {



        }
    });

DEBUG IMG WITH USER THAT HAS A CASINO NODE.enter image description here

DEBUG IMG WITH USER WHO DOES NOT HAVE A CASINO NODE. enter image description here

MY XML FILE

 <Button
                android:id="@+id/buyCassino"
                android:layout_width="183dp"
                android:layout_height="wrap_content"
                android:background="@color/colorAccent"
                android:text="Buy Cassino"
                android:textColor="#FFFFFF"
                android:visibility="invisible" />

            <Button
                android:id="@+id/openCassino"
                android:layout_width="190dp"
                android:layout_height="wrap_content"
                android:background="@color/colorAccent"
                android:text="OPEN CASSINO"
                android:textColor="#FFFFFF"
                android:visibility="invisible" />
1 Answers

Your JSON screenshot is not complete, but I'm going to assume that you have a structure /users/$uid, so where the UID node is directly under /users.

If that is the case, your listener is already listening for a specific user node, so you don't need to loop over dataSnapshot.getChildren().

So it should be something like:

ref.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.child("cassino").exists())  {
            openCassino.setVisibility(View.VISIBLE);
            buyCassino.setVisibility(View.GONE);
        } else {
            buyCassino.setVisibility(View.VISIBLE);
            openCassino.setVisibility(View.GONE);
        }
    }
    ...

A slightly cheaper way to accomplish the same is to listen for the cassino [sic] node directly:

ref.child("users").child(userId).child("cassino").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists())  {
            openCassino.setVisibility(View.VISIBLE);
            buyCassino.setVisibility(View.GONE);
        } else {
            buyCassino.setVisibility(View.VISIBLE);
            openCassino.setVisibility(View.GONE);
        }
    }
    ...

This saves having to load the entire user node only to use its cassino property.

Related