0

A very basic backstory here, I am creating an attendance monitoring app for my final year project. This class opens when a student scans a generated QR Code. When they hit yes, I want it to add +1 to their Present value. However, I'm having some issues with this, since this code I'm using here:

rootRef.child("Users").child(uid).child("present").setValue(+1);

It changes the child node Present in the Database to an Integer type, which causes a crash in the Profile.class because it's trying to display it as a String, that is what I want.

How do I convert this value back to a string once it's been updated to reflect the student's attendance?

-Edit--------

Thanks for the quick response, how would I then go about changing everything to make it an int. This is my User.java which creates the variable fields for the Database:


package com.example.finalyearproject;

public class User {

    //Initializing all of the variable names and values for the Database to upload to and pull from.

    public String name, email, year, course, role, absences, present;

    public User() {

    }

    public User(String name, String email, String year, String course, String role, String absences, String present) {

        //These will be used to upload and pull data to and from the database, mainly for the Profile.class

        this.name = name;
        this.email = email;
        this.year = year;
        this.course = course;
        this.role = role;
        this.absences = absences;
        this.present = present;

    }
}
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50

2 Answers2

1

which causes a crash in the Profile.class because it's trying to display it as a String

Why not change the type of the field in the class to be a number rather than a string. When you want to increment a field with a numeric value, the type of the field should always be a number. There's no method in the String class that allows you to increment a char sequence.

Besides that, when it comes to incrementing values in the Realtime Database, most likely you should use ServerValue.increment(1) as explained in my answer from the following post:

So in code that would be:

rootRef.child("Users").child(uid).child("present").setValue(ServerValue.increment(1));
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for the response, added a response in the question. – Chinwonder2 Mar 18 '22 at 12:46
  • Simply by updating multiple records at once, as explained in this [answer](https://stackoverflow.com/questions/59427847/edit-specific-values-in-firebase-database-using-android-studio), right? – Alex Mamo Mar 18 '22 at 12:48
0

Ok so I managed to figure this out for my specific project. It’s a bit basic and probably not very well optimised but it works lol.

I’ll leave it here for anyone who may have a similar issue. Thanks for all your guidance!


String present = userProfile.present;

int presentValue = Integer.parseInt(present);

presentValue = presentValue+1;

String finalPresentValue = Integer.toString(presentValue);

……

// Button OnclickListener


DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

rootRef.child(“Users”).child(uid).child(“present”).setValue(finalPresentValue);