0% found this document useful (0 votes)
6 views5 pages

Firebase Real-Time Database CRUD Operations

Android assignment

Uploaded by

Travel Yourself
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Firebase Real-Time Database CRUD Operations

Android assignment

Uploaded by

Travel Yourself
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

University of Central Punjab

Assignment # 4

Submitted to:

Prof. Moiz Iqbal

Submitted by:

Hiba Mustafa

F1F17BSCS0009

BSCS-4A

18 Feb, 2020
Real-time database Firebase: Update, Delete

//writeNewPost function for chat application


private fun Newpost(userId:String, username:String, title:String, body:String){
//creating new post at /user-posts/$userid/$postid simultaneously
//Using push key
val key=[Link]("posts").push().key
//Checking it is null or not
if(key==null){
Log.w(TAG,"Could not get push key for posts")
return
}
//Creating Post object
val post=Post(userId, username, title, body)
//Push it to a map
val postValues=[Link]()
val childUpdates=HashMap<String,Any>()
childUpdates["/posts/$key"]=postValues
childUpdates["/user-posts/$userId/$key"]=postValues
[Link](childUpdates)
}

Adding a Completion Callback:


[Link]("users").child(userId).setValue(user)
.addOnSuccessListener{
//Write was successful!
//?.
}
.addOnFailureListener{
//Write failed
//?
}

Delete:
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link].*
import [Link].activity_upd_del.*
import [Link].activity_welcome.*
import [Link].activity_welcome.email

class upd_del : AppCompatActivity() {

private var mFirebaseDatabase: DatabaseReference? = null


private var mFirebaseInstance: FirebaseDatabase? = null

private var userId: String? = null

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_upd_del)

mFirebaseInstance = [Link]()

// get reference to 'users' node


mFirebaseDatabase = mFirebaseInstance!!.getReference("users")

val user = [Link]().getCurrentUser()

// add it only if it is not saved to database


userId = user?.getUid()

private fun updateUser(name: String, email: String) {


// updating the user via child nodes
if (![Link](name) && ![Link](email)) {
mFirebaseDatabase!!.child(userId!!).child("name").setValue(name)
mFirebaseDatabase!!.child(userId!!).child("email").setValue(email)
[Link](applicationContext, "Successfully updated user",
Toast.LENGTH_SHORT).show()
}
else
[Link](applicationContext, "Unable to update user",
Toast.LENGTH_SHORT).show()

}
fun onUpdateClicked(view: View) {
val name = [Link]().toString()
val email = [Link]().toString()

//Calling updateUser function


updateUser(name, email)
}

fun onDeleteClicked(view: View) {


//Remove value from child
mFirebaseDatabase!!.child(userId!!).removeValue()
[Link](applicationContext, "Successfully deleted user",
Toast.LENGTH_SHORT).show()

// clear information
txt_user.setText("")
[Link]("")
[Link]("")
}

companion object {
private val TAG = upd_del::[Link]()
}
}

You might also like