Lab 8 Note
Lab 8 Note
[FrontEnd]
etFirstName etUserName
etLastName
btnRead
etAge
tvFirstName
etUserName
tvLastName
tvAge
btnInsert
[activity_insert_data.xml] [activity_read_data.xml]
btnCreateData
btnReadData
btnUpdateDat
a
btnDeleteData
[activity_main.xml]
etUserName etUserName
etFirstName
btnDelete
etLastName
etAge
btnUpdate
[activity_update_data.xml] [activity_delete_data.xml]
Prepared by Low Han Cheng
Step 2: Open the Firebase > Realtime Database (inside Build) >
Create Database > Set up location > Enable ‘Start in locked mode’
Step 3: Click inside Realtime Database > Rules > Edit rules, change
read & write to ‘true’ > Publish:
Step 3: Connect to Firebase > Add the Realtime Database SDK to your
app
[BackEnd]
Activity: MainActivity.kt
Activity: InsertData.kt
c) Create class file named as ‘User’, which contains the following properties:
data class User(
val firstName : String? = null,
val lastName : String? = null,
val age : String? = null,
val userName : String? = null){
}
d) Define ‘User’ variable which stored the inputs data that passed to the User data class
val User = User(firstName,lastName,age,userName)
Step 2: Insert data into Firebase database: Access this database path
a) Get instance of database and define the location
database = FirebaseDatabase.getInstance().getReference("Users")
b) Insert data and define actions for success or fail task execution
Child path named with the Insert User class data (firstName, lastName, age, username)
username filled by user
database.child(userName).setValue(User).addOnSuccessListener {
binding.etFirstName.text.clear()
binding.etLastName.text.clear() If task
binding.etAge.text.clear()
execution
binding.etUserName.text.clear()
success
Toast.makeText(this,"Successfully Saved", Toast.LENGTH_SHORT).show()
}.addOnFailureListener{ If task
Toast.makeText(this,"Failed", Toast.LENGTH_SHORT).show() execution
} fail
Prepared by Low Han Cheng
package com.example.firebasecrud
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.firebasecrud.databinding.ActivityInsertDataBinding
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
binding.btnInsert.setOnClickListener { Step 1
val firstName = binding.etFirstName.text.toString()
val lastName = binding.etLastName.text.toString()
val age = binding.etAge.text.toString()
val userName = binding.etUserName.text.toString()
database = FirebaseDatabase.getInstance().getReference("Users")
database.child(userName).setValue(User).addOnSuccessListener {
binding.etFirstName.text.clear() Step 2
binding.etLastName.text.clear()
binding.etAge.text.clear()
binding.etUserName.text.clear()
Toast.makeText(this,"Successfully Saved",
Toast.LENGTH_SHORT).show()
}.addOnFailureListener{
Toast.makeText(this,"Failed", Toast.LENGTH_SHORT).show()
}
}
}
}
Prepared by Low Han Cheng
Activity: ReadData.kt
}else{
b) Read data and define actions for success or fail task execution
database = FirebaseDatabase.getInstance().getReference("Users")
database.child(userName).get().addOnSuccessListener {
}
else{
Toast.makeText(this,"User Doesn't
Exist",Toast.LENGTH_SHORT).show()
}
If task
}.addOnFailureListener{ execution
Toast.makeText(this,"Failed",Toast.LENGTH_SHORT).show()
} fail
Prepared by Low Han Cheng
package com.example.firebasecrud
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.firebasecrud.databinding.ActivityInsertDataBinding
import com.example.firebasecrud.databinding.ActivityReadDataBinding
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
binding.btnRead.setOnClickListener {
Step 1
val userName : String = binding.etUsername.text.toString()
if (userName.isNotEmpty()){
readData(userName)
}else{
}
}
}
database = FirebaseDatabase.getInstance().getReference("Users")
database.child(userName).get().addOnSuccessListener { Step 2
if (it.exists()){
}.addOnFailureListener{
Toast.makeText(this,"Failed",Toast.LENGTH_SHORT).show()
}
}
}
Prepared by Low Han Cheng
b) Set up variables to store input data and pass them to updateData function
val userName = binding.etUserName.text.toString()
val firstName = binding.etFirstName.text.toString()
val lastName = binding.etLastName.text.toString()
val age = binding.etAge.text.toString()
updateData(userName,firstName,lastName,age)
b) ‘user’ variable which uses HashMap to pair up filled-in data with corresponding path
name in database
val user = mapOf<String,String>(
"firstName" to firstName,
"lastName" to lastName,
"age" to age
)
c) Update data and define actions for success or fail task execution
database = FirebaseDatabase.getInstance().getReference("Users")
database.child(userName).updateChildren(user).addOnSuccessListener {
}.addOnFailureListener{
Toast.makeText(this,"Failed to Update", If task
Toast.LENGTH_SHORT).show() execution
} fail
}
Prepared by Low Han Cheng
package com.example.firebasecrud
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.firebasecrud.databinding.ActivityReadDataBinding
import com.example.firebasecrud.databinding.ActivityUpdateDataBinding
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
binding.btnUpdate.setOnClickListener {
Step 1
val userName = binding.etUserName.text.toString()
val firstName = binding.etFirstName.text.toString()
val lastName = binding.etLastName.text.toString()
val age = binding.etAge.text.toString()
updateData(userName,firstName,lastName,age)
}
}
database = FirebaseDatabase.getInstance().getReference("Users")
database.child(userName).updateChildren(user).addOnSuccessListener
{
binding.etUserName.text.clear()
binding.etFirstName.text.clear()
binding.etLastName.text.clear()
binding.etAge.text.clear()
Toast.makeText(this,"Successfuly Updated",
Toast.LENGTH_SHORT).show()
}.addOnFailureListener{
Toast.makeText(this,"Failed to Update",
Toast.LENGTH_SHORT).show()
}
}
}
Prepared by Low Han Cheng
Activity: DeleteData.kt
b) Delete data and define actions for success or fail task execution
database = FirebaseDatabase.getInstance().getReference("Users")
database.child(userName).removeValue().addOnSuccessListener {
If task
binding.etUsername.text.clear() Delete data execution
Toast.makeText(this,"Deleted Successfully",
Toast.LENGTH_SHORT).show() success
}.addOnFailureListener{
Toast.makeText(this,"Failed to Delete", Toast.LENGTH_SHORT).show() If task
} execution
fail
Prepared by Low Han Cheng
package com.example.firebasecrud
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.firebasecrud.databinding.ActivityDeleteDataBinding
import com.example.firebasecrud.databinding.ActivityUpdateDataBinding
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
binding.btnDeleteData.setOnClickListener {
Step 1
val userName = binding.etUsername.text.toString()
if(userName.isNotEmpty()){
deleteData(userName)
}
else{
Toast.makeText(this,"Please enter the username",
Toast.LENGTH_SHORT).show()
}
}
}
database = FirebaseDatabase.getInstance().getReference("Users")
database.child(userName).removeValue().addOnSuccessListener {
binding.etUsername.text.clear()
Toast.makeText(this,"Deleted Successfully", Step 2
Toast.LENGTH_SHORT).show()
}.addOnFailureListener{
Toast.makeText(this,"Failed to Delete",
Toast.LENGTH_SHORT).show()
}
}
}