Count the number of records in the firebase

1

Good morning Developers!    I'm having a hard time on a project that I can not move forward, here's the problem / scenario:    We have a screen that brings information, a message, and on the screen there is the button to favor that message, this information is saved in the Firebase RealtimeDatabase, in this scenario I need the following elements: when the user clicks on the imLike save as tanned or uncaged depending on (it is working), every time check if that message had already been tanned by that user before (how the messages are generated on the screen randomly that check if the user had previously tanned should be done with the same frequency - is already working) , and finally I need a TextView to show how many tanned that message already has (that I still can not do).

The function below is called as soon as the activity is started and brings the following results: if the current user already gave like in that message the favorite image receives the red coloration and TextView tbm.

private void verificarCurtida(String versiculo, String usuario) {
    String cripto = CriptografiaBase64.criptografarVersiculo(versiculo);

    likeRef = ConfiguracaoFirebase.getFirebaseReference()
            .child("VERSICULOS")
            .child(cripto)
            .child("LIKES")
            .child(usuario);

    //Verificando se esse versiculo já foi curtido antes
    likeRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Pesquisando o idUsuarioe as curtidas
            if (dataSnapshot.getValue() != null){
                //Se retornar valor quer dizer que já fi curtida antes por esse usuário
                imLike.setImageResource(R.drawable.ic_favorite);
                tvLikes.setTextColor(Color.parseColor("#FF1E1E"));
            } else {
                imLike.setImageResource(R.drawable.ic_favorite_border);
                tvLikes.setTextColor(Color.parseColor("#000000"));
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

below is the same method, but with the attempt to add the user count on the tanned node:

private void verificarCurtida2(String versiculo, String usuario) {
    String cripto = CriptografiaBase64.criptografarVersiculo(versiculo);
    final int contar = 0;

    likeRef = ConfiguracaoFirebase.getFirebaseReference()
            .child("VERSICULOS")
            .child(cripto)
            .child("LIKES");
    //.child(usuario);

    //Verificando se esse versiculo já foi curtido antes
    likeRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Pesquisando o idUsuarioe as curtidas
            if (dataSnapshot.getValue() != null){
                for (DataSnapshot data: dataSnapshot.getChildren()){

                    if (data.getValue().equals(preferencias.getCHAVE_ID())){
                        //Se retornar valor quer dizer que já fi curtida antes por esse usuário
                        imLike.setImageResource(R.drawable.ic_favorite);
                        tvLikes.setTextColor(Color.parseColor("#FF1E1E"));
                        contar++;

                    } else {
                        imLike.setImageResource(R.drawable.ic_favorite_border);
                        tvLikes.setTextColor(Color.parseColor("#000000"));
                        contar++;
                    }

                }
                //tvLikes.setText((int) dataSnapshot.getChildrenCount());

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    tvLikes.setText(contar);

}

Below is the structure in firebase, where I have the following nodes: VERSICULES > IDVERSICULO > LIKES > ID's of users who clicked on like;

According to the image I need the following result: VERSICULES > IDVERSICULO > LIKES > 3 (TextView gets 3).

    
asked by anonymous 26.05.2018 / 13:55

0 answers