Saving Data on Android



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

build.gradle(Module:app)
 and add the following code into the 
android
 section:
testOptions { 
  unitTests { 
    includeAndroidResources = 
true
 
  } 
}
This will tell gradle to use the 
includeAndroidResources
 flag, and the name pretty 
much explains what that means! Next, add the following dependency in the 
dependencies
 section:
testImplementation 
"org.robolectric:robolectric:3.6.1"
Sync the gradle file after making these changes. Next, open 
ToDoDatabaseHandler.kt
 and add the following code inside the class, after 
deleteToDo()
:
// 1
 
fun
 
clearDbAndRecreate
()
 { 
  clearDb() 
  onCreate(writableDatabase) 

   
fun
 
clearDb
()
 { 
  writableDatabase.execSQL(
"DROP TABLE IF EXISTS 
$TABLE_NAME
"


 
// 2
 
fun
 
getAllText
()
: String { 
  
var
 result = 
""
 
  
val
 cols = arrayOf(KEY_TODO_NAME, KEY_TODO_IS_COMPLETED) 
  
val
 cursor = writableDatabase.query(TABLE_NAME, cols, 
      
null

null

null

null
, KEY_TODO_ID) 
  
val
 indexColumnName = 
cursor.getColumnIndexOrThrow(KEY_TODO_NAME) 
  
while
 (cursor != 
null
 && cursor.moveToNext()) { 
    result += cursor.getString(indexColumnName) 
  } 
 
  cursor.close() 
  
return
 result 
}
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
65


Here's what's happening above:
1. You will erase and recreate the database once the testing is done. This will allow 
the test to be run multiple times and ensure that residual test data isn't left in 
the database.
2.
getAllText()
 has one job, to create a 
String
 that consists of the TODO item 
names in the database all concatenated together. In the test you will write, you 
will see if the database contains the values you expect to see after inserting a few 
items.
Now, to create the test. Unit tests are contained in the 
test
 folder. Right click on 

Download 19,28 Mb.

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