Add +1 each time a Child is called Fire Base

Asked

Viewed 118 times

0

Hello, here I am again with the saga of fire base :p,

So how can I add +1 to child Users when someone enters an activity , in the case of a user profile, I want to do the same has in the profile here in the community, I thank you already.

I believe this is the starting point

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
        mPost_key = getIntent().getExtras().getString("uidusuario");

        mDatabase.child(mPost_key).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
        mDatabase.child(mPost_key).child("visitasperfil").setValue();
  • and what exactly is the doubt? I only touched the firebase for web, but the process I believe to be the same, as already made the "select" of the user is only a update() increasing the value, see the update doc for now I do so, also I am in search of a better way if there is.

  • by what I understood to send a pre-defined value

  • I need it to be constant, for now I will send Child to the firebase to create the profile then when a user see the profile uid for this Child there I will use getChildrenCount to count the number of items in Child,

1 answer

1


      mDatabaseVisitas.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Log.e(String.valueOf(dataSnapshot.child(post_key)),dataSnapshot.getChildrenCount() + "");

            for (DataSnapshot snap: dataSnapshot.getChildren()) {
                //Log.e(String.valueOf(snap.child(currentUserId).toString()),snap.getChildrenCount() + "");
                mVisitas.setText(dataSnapshot.child(currentUserId).getChildrenCount() + "");
            }}
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    final String user_id = mAuth.getCurrentUser().getUid();
    mDatabaseVisitas.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mDatabaseVisitas.child(currentUserId).child(mAuth.getCurrentUser().getUid()).setValue("true");

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.