On this screen, you can edit your post by taping on it. When you're done you can tap
an
Update
button to update the post content. By
tapping
Delete
button you can
delete the post if you're the author of the post. There's also a
Comments
section here.
The app also has the feature of adding a comment to the post which will be displayed
here. If you try to tap to any of these buttons you'll see that nothing happens. You'll
implement those functionalities next.
Upda/ng
Updating data in Realtime Database is almost the same as writing. You use the same
setValue()
method for updating. Add
POST_CONTENT_PATH
constant above
RealtimeDatabaseManager
class declaration:
private
const val
POST_CONTENT_PATH
=
"content"
You'll use this constant to indicate which field in the database you want to update.
Now
add the
updatePostContent
function:
fun updatePostContent(key:
String
, content:
String
) {
//1
database.getReference(
POSTS_REFERENCE
)
//2
.child(key)
//3
.child(
POST_CONTENT_PATH
)
//4
.setValue(content)
Saving Data on Android
Chapter 13: Reading to & Writing from Realtime Database
raywenderlich.com
222
}
1. First, you get a reference to the location of the posts in the database.
2. Here you use the key to access the location of the post you wan't to update.
3. You can create a new object and write the entire object to this location but that is
not needed. You can update a specific field in the post by specifying the path to
that field. Here you update only the content of the post.
4. Finally, you call
setValue
method with new content to update the content of the
post.
Open
PostDetailsActivity
class
and replace
TODO
in
updatePostButton.setOnClickListener
method in the
initializeClickListener
with this:
realtimeDatabaseManager.updatePostContent(post.id,
postText.text.
toString
().trim())
finish()
When the user taps on the
Update
button the post content will update and the
current activity will close.
Build and run your app. Open any post in the
list that was written by you, update the
post content, tap the
Update
button and verify both on the home screen and firebase
console that post content is updated.
Dele/ng
Deleting data in Realtime Database is very simple. You have two options. You can
delete data by using
setValue
method and specify
null
as
an argument or you can
use
removeValue()
method which will set the value at the specified location to
null
. You'll use the latter approach. Open
RealtimeDatabaseManager
class and add
the
deletePost
function:
fun deletePost(key:
String
) {
database.getReference(
POSTS_REFERENCE
)
.child(key)
.removeValue()
}
Here you get a reference to the location of the posts, then
you get the reference to
the desired posts and call
removeValue()
to delete it.
Open
PostDetailsActivity
and navigate to the
initializeClickListener()
Saving Data on Android
Chapter 13: Reading to & Writing from Realtime Database
raywenderlich.com
223
function and replace
TODO
in
on click listener of the
deletePostButton
with this:
realtimeDatabaseManager.deletePost(post.id)
finish()
Build and run your app. Open any post in the list that was written by you and click
the delete button. Verify on the home screen and firebase console that the post is
deleted.
Do'stlaringiz bilan baham: