[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

Unit V-QB

The document outlines various methods for data persistence in Android, including SharedPreferences, file systems, and SQLite databases. It provides code snippets for storing and retrieving data, as well as explanations of key concepts such as internal and external storage, ContentValues, and the role of DBAdapter. Additionally, it includes tasks related to implementing context menus, GridViews, and user interactions in Android applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Unit V-QB

The document outlines various methods for data persistence in Android, including SharedPreferences, file systems, and SQLite databases. It provides code snippets for storing and retrieving data, as well as explanations of key concepts such as internal and external storage, ContentValues, and the role of DBAdapter. Additionally, it includes tasks related to implementing context menus, GridViews, and user interactions in Android applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

UNIT – 4 & 5

PART A

1. List out the ways to persist data.


o A lightweight mechanism known as shared preferences to save small chunks
of data
o Traditional file systems
o A relational database management system through the support of SQLite
databases

2. How does the SharedPreferences object helps to store data?


The SharedPreferences object enables you to store data that is best stored as
name/value pairs— for example, user ID, birth date, gender, driver’s license number,
and so on.

3. Give the code to store the string “kongu” in your internal storage.
String str = textBox.getText().toString();
FileOutputStream fOut = openFileOutput("textfile.txt",
MODE_PRIVATE); OutputStreamWriter osw = new
OutputStreamWriter(fOut); //---write the string to the file---
osw.write(str);
osw.flush();
osw.close();
4. Give the code to read the string from the internal storage of the device.
FileInputStream fIn = openFileInput("textfile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = ""; int charRead;
while ((charRead = isr.read(inputBuffer)) > 0)
{ //---convert the chars to a String---
String readString = String.copyValueOf(inputBuffer, 0, charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE]; } //---set the EditText to the
text that has been // read---
textBox.setText(s).

Note: To save text into a file, you use the FileOutputStream class. The
openFileOutput() method opens a named file for writing, with the mode
specified. In this example, you use the MODE_PRIVATE constant to indicate
that the file is readable by all other applications: FileOutputStream fOut =
openFileOutput("textfile.txt", MODE_PRIVATE); To convert a character
stream into a byte stream, you use an instance of the OutputStreamWriter
class, by passing it an instance of the FileOutputStream object
OutputStreamWriter osw = new OutputStreamWriter(fOut);
5. Difference between flush() and close().
To ensure that all the bytes are written to the file, use the flush()
method. The close() method to close the file.
6. Name the classes used for reading the content from a file.
FileInputStream fIn = openFileInput("textfile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
7. What is the use of getExternalStorageDirectory()?
Return the full path to the external storage. Typically, it should return the “/sdcard”
path for a real device, and “/mnt/sdcard” for an Android emulator.

8. How to choose the best storage option for your application?


If you have data that can be represented using name/value pairs, then use the
SharedPreferences object.
If you need to store ad-hoc data then using the internal storage is a good option. To
share your application data with other users, store your files on the SD card of the
device so that users can easily transfer the data to other devices (and computers).

9. List out the methods to be implemented in DBAdapter class.


onCreate() → creates a new database if the required database is not present.
onUpgrade()→called when the database needs to be upgraded.

10. Show the use of ContentValues.


A ContentValues object is used to store name/value pairs.
Its put() method enables you to insert keys with values of different data
types. Example:
ContentValues initialValues = new ContentValues(); initialValues.put(KEY_NAME,
name); initialValues.put(KEY_EMAIL, email); return
db.insert(DATABASE_TABLE, null, initialValues);
11. What are the parameters used in the CursorLoader class.
12. What is use of cursor?
13. Write the statement to add in the manifest file to give permission to access
the external files.
14. What is the purpose of SharedPreferences in Android?
15. Differentiate between internal storage and external storage in Android.
16. Explain the use of getSharedPreferences() method in an Activity.
17. What is the role of a DBAdapter helper class in Android SQLite database management?
18. Why is it important to choose the appropriate storage option in an Android app?
19. Define internal storage. When would you prefer using it?
20. List the steps required to persist simple key-value data using SharedPreferences.
21. Explain the structure of an SQLite database in Android.
22. What are the key differences between using files and databases in Android for data
storage?
23. Describe how external storage access permissions are managed in Android.
24. Write a code snippet to store a user's name and email using SharedPreferences.
25. How would you retrieve a boolean value from SharedPreferences in an Activity?
Provide a code example.
26. Write a code snippet to save a string to a file in internal storage.
27. Demonstrate how to write data to a file in external storage.
28. Write a function to read a text file from internal storage.
29. How would you insert a new row into an SQLite database using a DBAdapter
class?
30. Provide the code to create a table in SQLite using SQLiteOpenHelper.
31. How do you update a preference value in SharedPreferences? Show a code
snippet.
32. Illustrate how to initialize and use an SQLite database helper class in an Activity.
33. Write code to check if external storage is available for writing.

PART B:
1. Create a context menu for a ListView that allows users to delete or edit an item.
How would you implement this in your activity?

2. Apply a long-press gesture to a TextView to show a context menu with formatting


options like Bold, Italic, and Underline.

3. Implement a context menu for an image gallery app, allowing users to “Share”,
“Delete”, or “Set as Wallpaper” when an image is long-pressed.

4. Create a GridView that displays a collection of images from the drawable folder.
How would you implement the adapter to populate the GridView?

5. Develop an app screen that shows a GridView of products (image + name). On


clicking an item, navigate to another activity which have a detailed view of the
selected product.

6. Create an ImageSwitcher that changes images when a “Next” button is clicked.


Load the images from the drawable folder.

7. How to access preferences using activity?


8. How to display and modify shared preferences value?
9. what are the traditional way of storing data in android?
10. create an blood bank application. Collect the required data from the user and store
them in the database.

You might also like