Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet125/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   121   122   123   124   125   126   127   128   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

@Embedded
The 
@Embedded
 annotation can be used on an entity's field to tell Room that the 
properties on the annotated object should be represented as columns on the same 
entity.
For example, say that you have a 
User
 table that contains address information. Your 
entity could look like this:
@Entity
 
data
 
class
 
User

    
@PrimaryKey
 
val
 id: 
Int
,
Saving Data on Android
Chapter 6: Entity Definitions
raywenderlich.com
110


    
val
 firstName: String?, 
    
val
 street: String?, 
    
val
 state: String?, 
    
val
 city: String?, 
    
@ColumnInfo(name = "post_code")
 
val
 postCode: 
Int
 
)
However, thanks to the 
@Embedded
 annotation you can represent the address fields 
as a separate class but Room will still generate a single table:
data
 
class
 
Address

    
val
 street: String?, 
    
val
 state: String?, 
    
val
 city: String?, 
    
@ColumnInfo(name = "post_code")
 
val
 postCode: 
Int
 

 
@Entity
 
data
 
class
 
User

    
@PrimaryKey
 
val
 id: 
Int

    
val
 firstName: String?, 
    
@Embedded
 
val
 address: Address? 
)
Both implementations will generate a single 
User
 table with six fields: 
id

firstName

street

state

city
 and 
postCode
.
There are more annotations available, but these are by far the most commonly used 
when creating your entities.
Now that you know the theory, it is time to put it into practice!
Crea/ng your en//es
Since you'll be working on with Room, make sure you've opened the 
starter
 project, 
located in the 
entity-definitions 

 projects
 folder.
Now, once Android studio finishes indexing the code, you need to add the 
appropriate dependencies to your 
build.gradle
 files to use Room. To ensure forward 
compatibility with future versions of Android, you will use 
Androidx
 artifacts for 
this project.
Saving Data on Android
Chapter 6: Entity Definitions
raywenderlich.com
111


Open the app-level 
build.gradle
 file and add the following lines under the 
dependencies block:
//Room
 
implementation 
"androidx.room:room-common:$room_version"
 
kapt 
"androidx.room:room-compiler:$room_version"
 
implementation 
"androidx.room:room-runtime:$room_version"
Press 
Sync Now
 and wait until Android Studio finishes syncing your new 
dependencies.
Build and run your app one more time just to make sure that the project is still 
working properly after the updates to your gradle files.
Saving Data on Android
Chapter 6: Entity Definitions
raywenderlich.com
112


Create a new package under the root directory of your app by right-clicking on 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   121   122   123   124   125   126   127   128   ...   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