l_b
l_b

Reputation: 583

How can I check if a value is there in my firebase database?

I am making a Question Answer app. Users can like a question. If a user likes it then in the firebase it is stored in likedBy. I have kept a check to see if a user has already liked the question, and if yes, then the like button will be green in color. But that is not happening. And when I click the like button again there are two people who have liked, with same username. Please help.

Firebase Database

databaseReference.child("questions").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.exists()) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    if (dataSnapshot.child("question").getValue().toString().equals(question) && dataSnapshot.child("askedBy").getValue().toString().equals(username)
                            && dataSnapshot.child("likes").getValue().toString().equals(likes)) {
                        if (dataSnapshot.child("likedBy").hasChild(currentUser[0])) {
                            liked = true;
                            like.setImageResource(R.drawable.like_selected);
                        } else {
                            liked = false;
                            like.setImageResource(R.drawable.like_not_selected);
                        }
                    }
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

    like.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (liked) {
                databaseReference.child("questions").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if (snapshot.exists()) {
                            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                                if (dataSnapshot.child("question").getValue().toString().equals(question) && dataSnapshot.child("askedBy").getValue().toString().equals(username)
                                        && dataSnapshot.child("likes").getValue().toString().equals(likes)) {
                                    dataSnapshot.getRef().child("likedBy").child(currentUser[0]).removeValue();
                                    int liken = Integer.parseInt(likes) - 1;
                                    dataSnapshot.child("likes").getRef().setValue(liken).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()) {
                                                likes = Integer.toString(liken);
                                                numberOfLikes.setText(likes);
                                                like.setImageResource(R.drawable.like_not_selected);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
            } else {
                databaseReference.child("questions").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if (snapshot.exists()) {
                            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                                if (dataSnapshot.child("question").getValue().toString().equals(question) && dataSnapshot.child("askedBy").getValue().toString().equals(username)
                                        && dataSnapshot.child("likes").getValue().toString().equals(likes)) {
                                    int numberOfUsers = (int) dataSnapshot.child("likedBy").getChildrenCount() + 1;
                                    dataSnapshot.child("likedBy").child(String.valueOf(numberOfUsers)).getRef().setValue(currentUser[0]);
                                    int liken = Integer.parseInt(likes) + 1;
                                    dataSnapshot.child("likes").getRef().setValue(liken);
                                    databaseReference.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                                            if (snapshot.exists()) {
                                                for (DataSnapshot dataSnapshot1 : snapshot.getChildren()) {
                                                    if (dataSnapshot1.child("username").getValue().toString().equals(username)) {
                                                        int points = Integer.parseInt(dataSnapshot1.child("points").getValue().toString()) + 5;
                                                        numberOfPointsGained += 5;
                                                        dataSnapshot1.child("points").getRef().setValue(points).addOnCompleteListener(new OnCompleteListener<Void>() {
                                                            @Override
                                                            public void onComplete(@NonNull Task<Void> task) {
                                                                if (task.isSuccessful()) {
                                                                    likes = Integer.toString(liken);
                                                                    numberOfLikes.setText(likes);
                                                                    like.setImageResource(R.drawable.like_selected);
                                                                    leaderBoard();
                                                                }
                                                            }
                                                        });
                                                    }
                                                }
                                            }
                                        }

                                        @Override
                                        public void onCancelled(@NonNull DatabaseError error) {

                                        }
                                    });
                                }
                            }
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
            }
        }
    });

Thanks in advance.

Upvotes: 0

Views: 54

Answers (2)

Cyrille Con Morales
Cyrille Con Morales

Reputation: 957

If your talking to how to check the value you can go like this

orderByChild you are looking for

ref.child("Users").orderByChild("IDorKEY").equalTo("nameORkeyID").once("value",snapshot => {
   if (snapshot.exists()){
    //Existed dont add or do something
    }
});

Upvotes: 1

stic-lab
stic-lab

Reputation: 553

I'm not a firebase professional but I think you can use this logic:

First create a function called onClickLikeBtn. Then inside this function, you have to switch the state of the like according to the existing value. For exemple, if the actual state is like==true, you have to switch it to like==false. Bellow is an sample function that can be use for this logic:

public function onClickLikeBtn (Boolean like){
  return !like;
}

Upvotes: 0

Related Questions