Home > Software design >  convert a single firestore firebase field from string to int
convert a single firestore firebase field from string to int

Time:04-20

I've managed to store data inside firebase using cloud firestore

I've created a collection that can stores the users extra details like their age, height, and weight

There's a feature in my app that is able to calculate the user's drinking water rate by day based on their weight, the calculation is user's weight divided by 30

So when the user open the page it will directly calculate it.

However. The type of data that stored in firestore is identified as String, and I've been trying to convert the data field type to Int so the program will execute. But I got errors like

java.lang.NumberFormatException: For input string: "com.google.firebase.firestore.DocumentReference@a4443fb"

public class drinkingWater extends BaseMainMenu {

    TextView cRslt;

    FirebaseAuth mAuth;
    FirebaseFirestore mStore;
    String userID;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drinking_water);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        cRslt = findViewById(R.id.result);

        mAuth = FirebaseAuth.getInstance();
        mStore = FirebaseFirestore.getInstance();
        userID = mAuth.getCurrentUser().getUid();

        DocumentReference docRef = mStore.collection("userDetail").document(userID);
        docRef.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                int n1 = Integer.valueOf(String.valueOf(mStore.collection("userDetail").document(userID   "Weight")));
                int n2 = 30;
                int sum = n1 / n2;
                cRslt.setText(String.valueOf(sum));



            }
        });

    }
}

CodePudding user response:

If I read your code correctly you have a Weight field in the document, and you want to get the value of that field. You can get the data from the DocumentSnapshot object with:

documentSnapshot.get("Weight)

So if Weight is stored as an integer value, that'd be:

int n1 = documentSnapshot.get("Weight)

And if Weight is stored as a string representation of an int value, it'd be:

int n1 = Integer.valueOf(documentSnapshot.get("Weight));

CodePudding user response:

The error is saying that you cannot convert a string of type text to a direct integer. If your string contains any text, it cannot be converted to a number. And I see that you are also parsing UserId,"userDetail into Integer which is impossible. First, you take all the text out of your string in another variable and after then you can easily convert the variable containing weight from string to integer. make sure that your weight variable is only in number format.

  • Related