Home > Blockchain >  Need to update one field in firebase kotlin?
Need to update one field in firebase kotlin?

Time:09-29

I need to update only one field with sum of existing and new enter amount. How to do that?

I haven't idea to continue this.

val value1 = amountText.getText().toString().toDouble()
                            val value2 = rootRef.child("BankDetails").child(sessionId).child("accountBalance")
                            var valueT = value1   value2

I need to update only account balance in bank details. I'm still learning kotlin.

Edited

data class BankDetails (val accountBalance:Double,  val bankAccNo:String,val emailAddress:String ){
   
    override fun toString(): String {
        return "${this.emailAddress}, ${this.bankAccNo}"
    }
    
    fun toDouble() {
         this.accountBalance
    }
    
}

BankDetails

{
  "BankDetails" : {
    "aaa@gmail,com" : {
      "accountBalance" : 200,
      "bankAccNo" : 12345678,
      "emailAddress" : "aaa@gmail,com"
    },
    "abi@gmail,com" : {
      "accountBalance" : 3000,
      "bankAccNo" : "123xxxxx",
      "emailAddress" : "abi@gmail,com"
    }
  },
  "User" : {
    "abi@gmail,com" : {
      "age" : 23,
      "bankAccNo" : "123xxxxxxx",
      "emailAddress" : "abi@gmail,com",
      "fullName" : "Abi",
      "passwordRegister" : "123456",
      "phoneNo" : 713615554
    }
  }
}

CodePudding user response:

To increment the value of accountBalance with the value of amountText, do it as simple as:

val value1 = amountText.getText().toString().toDouble()
val accountBalanceRef = rootRef.child("BankDetails").child(sessionId).child("accountBalance")
accountBalanceRef.setValue(ServerValue.increment(value1))

If you want, you can also attach a complete listener to the setValue() operation to see if something goes wrong.

  • Related