Saving Data on Android


update an existing record



Download 19,28 Mb.
Pdf ko'rish
bet69/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   65   66   67   68   69   70   71   72   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

update an existing record
, and finally, to 
delete records
.
Upda/ng a TODO
To add the capability to update a record, replace the line of code 
return 0
 in the 
updateToDo
 function with the code below:
// 1
 
val
 todoId = toDo.toDoId.toString() 
// 2
 
val
 db: SQLiteDatabase = writableDatabase 
// 3
 
val
 values = ContentValues() 
values.put(KEY_TODO_NAME, toDo.toDoName) 
values.put(KEY_TODO_IS_COMPLETED, toDo.isCompleted) 
// 4
 
return
 db.update(TABLE_NAME, values, 
"
$KEY_TODO_ID
=?"

arrayOf(todoId))
With this code, you:
1. Prepare the 
todoId
 argument, for the 
Update
 clause.
2. Get a writable instance of the database.
3. Create a 
ContentValues
 object to contain the key/value pairs, and put the TODO 
item's property values in it.
4. Run the update query on the database and return the result of the operation.
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
62


Run the app. Tap the 
edit icon
 that looks like a pencil next to the record. Update the 
record to a new value and tap 
Update
.
Update a record
Now, the record should reflect any changes you made as it is displayed in the 
RecyclerView
.
Updated record
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
63


Note
: The update function you just completed is used in multiple places in the 
app. First, it was used in the previous example when changes were made to the 
name of the TODO item. The app also updates TODO items when the 
completed checkbox is checked.
There is one more bit of functionality to add to this app. The app must also be able to 
delete TODO items.
Dele/ng a TODO
In order to delete an item from the database, add the following code to the 
deleteToDo
 function:
val
 db: SQLiteDatabase = writableDatabase 
db.delete(TABLE_NAME, 
"
$KEY_TODO_ID
=?"
, arrayOf(id.toString())) 
db.close()
The above section of code simply gets an instance of the writeable database, runs a 
Delete
 command against it utilizing the correct TODO item 
id
, and closes the 
database. Run the app and tap the 
Delete
 icon that looks like a wastebasket and the 
TODO item magically disappears.
You have successfully written all the CRUD operations for this TODO list app! Now, 
before you are finished, you will add a unit test to the program with 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   65   66   67   68   69   70   71   72   ...   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