Saving Data on Android


Reading and wri/ng the prefs from



Download 19,28 Mb.
Pdf ko'rish
bet51/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   47   48   49   50   51   52   53   54   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Reading and wri/ng the prefs from 
MainAc/vity
Right now, in 
MainActivity.kt
, you're using hardcoded values for the sort order and 
the priority filters, which you pass on to the 
NoteAdapter
, to display notes in a 
different order and filter which notes should be in the list. The right way to do this, 
which involves 
SharedPreferences
, is to read a user's preferred ways of sorting and 
filtering, from prefs, and then passing those values to the 
NoteAdapter
.
To do this, change the way you create the 
priorities
 
Set
 by changing the 
statement to the following:
private
 
val
 priorities 
by
 lazy 
{ notePrefs.getNotePriorityFilters().toMutableSet() }
Instead of creating an empty mutable set and then adding values to it, you're now 
reading from the preferences, in a lazy way. Once you create the 
NoteAdapter
, the 
value will be assigned and you can use the 
priorities
.
Another change you have to make is the creation of the 
NoteAdapter
:
private
 
val
 noteAdapter: NoteAdapter 
by
 lazy { 
  NoteAdapter(
this

      priorities, 
      notePrefs.getNoteSortOrder(), 
// Read from preferences
 
      ::showEditNoteDialog 
  ) 
}
Instead of passing in a hardcoded value for 
FILENAME_ASC
 as the 
NoteSortOrder

you're reading from the preferences to get the ordering the user previously selected.
Note
: Do not attempt to modify a string set read from the prefs. The results 
can be unpredictable. Always make a copy of the set instead, if it is going to be 
modified, like in the case of this app. You can do that by calling 
toMutableSet()
, because Kotlin internally creates a new object, instead of 
just copying the 
Set
 value.
Saving Data on Android
Chapter 2: Shared Preferences
raywenderlich.com
46


The app can now load the saved preferences when the app launches, but it also needs 
to store the values anytime the menu options change. To do that, find 
updateNoteSortOrder()
 and insert the following, replacing the 
TODO
:
notePrefs.saveNoteSortOrder(sortOrder)
The line of code above will save the 
sortOrder
 to the prefs. Finally, find 
updateNotePrioritiesFilter()
 at the bottom of the 
MainActivity
 and insert this 
line of code, once again replacing the 
TODO
:
notePrefs.saveNotePriorityFilters(priorities)
The above statement will save the list of priority filters to the prefs. If you look in 
onOptionsItemSelected()
, the above two functions you just wrote the code for are 
called anytime the appropriate menu options are changed.
Finally, run the app. You can now filter and sort notes to your heart's content and the 
app will save your personalized preferences. Once you come back, everything will be 
ready and waiting for you!

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   47   48   49   50   51   52   53   54   ...   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