Saving Data on Android


Wri/ng to internal storage



Download 19,28 Mb.
Pdf ko'rish
bet21/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   17   18   19   20   21   22   23   24   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Wri/ng to internal storage
Open 
MainActivity.kt
. Notice the code immediately above 
onCreate()
:
private
 
val
 repo: NoteRepository 
by
 lazy 
{ InternalFileRepository(
this
) }
This is a lazy value. It represents an object of a class that implements 
NoteRepository
. There’s a separate implementation for each storage type 
demonstrated in this chapter.
The 
repo
 is initialized the first time it’s used and will be utilized throughout 
MainActivity
. This includes the button click events that call the add, get and delete 
methods required by 
NoteRepository
.
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
22


In 
onCreate()
, locate 
btnWrite.setOnClickListener
 add the following code for 
the 
WRITE
 button’s click event:
// 1
 
if
 (edtFileName.text.isNotEmpty()) { 
  
// 2
 
  
try
 { 
    
// 3
 
    repo.addNote(Note(edtFileName.text.toString(), 
        edtNoteText.text.toString())) 
  } 
catch
 (e: Exception) { 
// 4
 
    showToast(
"File Write Failed"

  } 
  
// 5
 
  edtFileName.text.clear() 
  edtNoteText.text.clear() 

else
 { 
// 6
 
  showToast(
"Please provide a Filename"

}
Here’s how it works:
1. Use an 
if/else
 statement to ensure the user entered the required information.
2. Put 
repo.addNote
 into a 
try/catch
 block. Writing a file can fail for different 
reasons like permissions or trying to use a disk with not enough space available.
3. Call 
addNote()
, passing in a 
Note
 that contains the filename and text provided 
in the 
EditText
 fields.
4. If writing the file fails, display a 
Toast
 message and write the stacktrace of the 
error to Logcat.
5. To prepare the interface for the next operation, clear the text from 
edtFileName
 
and 
edtNoteText
.
6. If the user did not enter a filename, display a toast message within the 
else
 
block. 
showToast
 is a utility function that exists in 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   17   18   19   20   21   22   23   24   ...   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