Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet225/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   221   222   223   224   225   226   227   228   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Upda/ng and dele/ng data
Tap on a post on the home screen to open another screen which shows post details:
Saving Data on Android
Chapter 13: Reading to & Writing from Realtime Database
raywenderlich.com
221


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.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   221   222   223   224   225   226   227   228   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish