Saving Data on Android



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

ToDoDatabaseHandler.kt
 file in the 
readToDos
 function replace the line of 
code 
return ArrayList()
 with the following code:
// 1
 
val
 db: SQLiteDatabase = readableDatabase 
// 2
 
val
 list = ArrayList() 
// 3
 
val
 selectAll = 
"SELECT * FROM 
$TABLE_NAME
"
 
// 4
 
val
 cursor: Cursor = db.rawQuery(selectAll, 
null

// 5
 
if
 (cursor.moveToFirst()) { 
  
do
 { 
    
// 6
 
    
val
 toDo = ToDo().apply { 
      toDoId = 
cursor.getLong(cursor.getColumnIndex(KEY_TODO_ID)) 
      toDoName = 
cursor.getString(cursor.getColumnIndex(KEY_TODO_NAME)) 
      isCompleted = 
cursor.getInt(cursor.getColumnIndex(KEY_TODO_IS_COMPLETED)) == 
1
 
    } 
    
// 7
 
    list.add(toDo) 
  } 
while
 (cursor.moveToNext()) 

// 8
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
60


cursor.close() 
// 9
 
return
 list
With the above, you:
1. Get a readable instance of the database.
2. Create an 
ArrayList
 to store the records.
3. Construct the 
Select
 query to get the records.
4. Create a 
Cursor
 using the 
Select
 query on the database.
5. Starting at the 
beginning
, use the 
Cursor
 to move through all the records one at 
a time.
6. Assign the fields of each record to the corresponding attribute of a new 
TODO
 
item.
7. Add the 
TODO
 item to the 
list
.
8. Close the cursor, to avoid memory leaks
9. Return the list of 
TODO
 items as a result.
Now, run the app, and the record added in the previous step can be displayed.
The added to-do item
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
61


So far, you've added a lot of code to the project. The app is now able to create a 
database, update the database, add records and read the records. Next, you will add 
the capability to 

Download 19,28 Mb.

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