sharedpreferences internal storage external storage€¦ · // fragment that displays the username...
TRANSCRIPT
![Page 1: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/1.jpg)
![Page 2: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/2.jpg)
![Page 3: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/3.jpg)
SharedPreferencesInternal StorageExternal StorageSQLite databases
![Page 4: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/4.jpg)
Use when you want to store small amounts of primitive data
![Page 5: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/5.jpg)
A persistent map that holds key-value pairs of simple data typesAutomatically persisted across application sessions
![Page 6: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/6.jpg)
Often used for long-term storage of customizable application data, such as:
Account name
Favorite WiFi networks
User settings
![Page 7: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/7.jpg)
Get a SharedPreference Object associated with a given ActivityActivity.getPreferences (int mode)
MODE_PRIVATE is default mode
![Page 8: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/8.jpg)
Get named SharedPreferences fileSingle SharedPreference object for a given nameContext.getSharedPreferences (
String name, int mode)name – name of SharedPreferences file
mode – e.g., MODE_PRIVATE
![Page 9: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/9.jpg)
Call SharedPreferences.edit()Returns a SharedPreferences.Editor instance
![Page 10: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/10.jpg)
Use SharedPreferences.Editor instance to add values to SharedPreferencesputInt(String key, int value)putString(String key, String value)remove(String key)
![Page 11: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/11.jpg)
Commit edited values withSharedPreferences.Editor.commit()
![Page 12: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/12.jpg)
Use SharedPreferences methods to read valuesgetAll()getBoolean(String key, …) getString(String key, …)
![Page 13: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/13.jpg)
When the user presses the play button, the application displays a random numberThe application keeps track of the highest number seen so far
![Page 14: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/14.jpg)
DataManagementSharedPreferences
![Page 15: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/15.jpg)
public class SharedPreferenceReadWriteActivity extends Activity {…
public void onClickPlayButton(View v) {…// Get Stored High Scoreif (val > mPrefs.getInt(HIGH_SCORE_KEY, 0)) {
// Get and edit high scoreSharedPreferences.Editor editor = mPrefs.edit();editor.putInt(HIGH_SCORE_KEY, val);editor.apply();…
![Page 16: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/16.jpg)
…// Reset Buttonpublic void onClickResetButton (View v) {
// Set high score to 0SharedPreferences.Editor editor = prefs.edit();editor.putInt(HIGH_SCORE_KEY, 0);editor.apply();…
![Page 17: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/17.jpg)
A Class that supports displaying & modifying user preferences
![Page 18: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/18.jpg)
This application displays a PreferenceFragment, which allows the user to enter and change a persistent user name
![Page 19: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/19.jpg)
DataManagementPreferenceFragment
![Page 20: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/20.jpg)
<?xml version="1.0" encoding="utf-8"?><fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/userPreferenceFragment"class="course.examples.datamanagement.preferencefragment.
ViewAndUpdatePreferencesActivity$UserPreferenceFragment"android:layout_width="match_parent"android:layout_height="match_parent" />
![Page 21: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/21.jpg)
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreferenceandroid:dialogMessage="Enter Your User Name"android:dialogTitle="Change User Name"android:key="uname"android:negativeButtonText="Cancel"android:positiveButtonText="Submit"android:title="User Name"/>
</PreferenceScreen>
![Page 22: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/22.jpg)
// Fragment that displays the username preferencepublic static class UserPreferenceFragment extends PreferenceFragment {
…public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);// Load the preferences from an XML resourceaddPreferencesFromResource(R.xml.user_prefs);// Get the username PreferencemUserNamePreference = getPreferenceManager() .findPreference(USERNAME); // Attach a listener to update summary when username changesmListener = new OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
mUserNamePreference.setSummary(sharedPreferences.getString(USERNAME, "None Set")); }
};
![Page 23: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/23.jpg)
// Get SharedPreferences object managed by the PreferenceManager for this FragmentSharedPreferences prefs = getPreferenceManager() .getSharedPreferences();
// Register a listener on the SharedPreferences objectprefs.registerOnSharedPreferenceChangeListener(mListener);
// Invoke callback manually to display the current usernamemListener.onSharedPreferenceChanged(prefs, USERNAME);
}}
}
![Page 24: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/24.jpg)
Use when you want to store small to medium amounts of private data
![Page 25: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/25.jpg)
Use when you want to store larger amounts of non-private data
![Page 26: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/26.jpg)
Class that represents a file system entity identified by a pathname
![Page 27: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/27.jpg)
Storage areas are classified as internal or externalInternal memory usually used for smaller, application private data setsExternal memory usually used for larger, non-private data sets
![Page 28: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/28.jpg)
FileOutputStream openFileOutput (String name, int mode)Open private file for writing. Creates the file if it doesn't already exist
FileInputStream openFileInput (String name)Open private file for reading
Many others. See documentation.
![Page 29: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/29.jpg)
If a text file does not already exist, application writes text to that text fileApplication then reads data from the text file and displays it
![Page 30: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/30.jpg)
DataManagementFileInternalMemory
![Page 31: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/31.jpg)
private final static String fileName = "TestFile.txt";public void onCreate(Bundle savedInstanceState) {
if (!getFileStreamPath(fileName).exists()) {try { writeFile(); }catch (FileNotFoundException e) { …}
}
try { readFileAndDisplay(textView); } catch (IOException e) { …}
}
![Page 32: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/32.jpg)
private void writeFile() throws FileNotFoundException {
FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);PrintWriter pw = new PrintWriter(new BufferedWriter( new OutputStreamWriter(fos)));
pw.println("Line 1: This is a test of the File Writing API");pw.println("Line 2: This is a test of the File Writing API");pw.println("Line 3: This is a test of the File Writing API");
pw.close();
}
![Page 33: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/33.jpg)
private void readFileAndDisplay(TextView tv) throws IOException {
FileInputStream fis = openFileInput(fileName);BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;String sep = System.getProperty("line.separator");
while (null != (line = br.readLine())) {tv.append(line + sep);
}
br.close();
}
![Page 34: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/34.jpg)
Removable media may appear/disappear without warning
![Page 35: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/35.jpg)
String Environment.getExternalStorageState()Returns
MEDIA_MOUNTED - present & mounted with read/write access
MEDIA_MOUNTED_READ_ONLY - present & mounted with read-only access
MEDIA_REMOVED - not present
![Page 36: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/36.jpg)
Permission to write external files<uses-permission android:name=
“android.permission.WRITE_EXTERNAL_STORAGE"/>
![Page 37: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/37.jpg)
If not done already, application reads an image file from its /res/raw directory
Copies that file to external storage
Application then reads image data from the file in external storage and then displays the image
![Page 38: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/38.jpg)
DataManagementFileExternalMemory
![Page 39: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/39.jpg)
public void onCreate(Bundle savedInstanceState) {…
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {String fileName = "painter.png";File outFile = new File (getExternalFilesDir(Environment.DIRECTORY_PICTURES),
fileName);if (!outFile.exists()) copyImageToMemory(outFile);
ImageView imageview = findViewById(R.id.image);imageview.setImageURI(Uri.parse("file://" + outFile.getAbsolutePath()));
}}
![Page 40: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/40.jpg)
private void copyImageToMemory(File outFile) {try {
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(outFile));
BufferedInputStream is = new BufferedInputStream(getResources().openRawResource(R.raw.painter));
copy(is, os);
} catch (FileNotFoundException e) { … }}
![Page 41: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/41.jpg)
private void copy(InputStream is, OutputStream os) {final byte[] buf = new byte[1024];int numBytes;try {
while (-1 != (numBytes = is.read(buf))) {os.write(buf, 0, numBytes);
}} catch (IOException e) {
e.printStackTrace();} finally {
try {is.close();os.close();
} catch (IOException e) { …}}
}}
![Page 42: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/42.jpg)
Temporary files that may be deleted by the system when storage is lowThese files are removed when application is uninstalled
![Page 43: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/43.jpg)
File Context.getCacheDir()Returns absolute path to an application-specific directory that can be used for temporary files
![Page 44: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/44.jpg)
Context.getExternalCacheDir()returns a File representing external storage directory for cache files
![Page 45: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/45.jpg)
Use when you want to store store small to large amounts of private, structured data
![Page 46: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/46.jpg)
SQLite provides in-memory databaseDesigned to operate within a very small footprint (e.g., <300kB) Implements most of SQL92Supports ACID transactions
Atomic, Consistent, Isolated & Durable
![Page 47: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/47.jpg)
Recommended method relies on a helper class called SQLiteOpenHelper
![Page 48: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/48.jpg)
Subclass SQLiteOpenHelperCall super() from subclass constructor to initialize underlying database
![Page 49: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/49.jpg)
Override onCreate() Override onUpgrade()Execute CREATE TABLE commands
![Page 50: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/50.jpg)
Use SQLiteOpenHelper methods to open & return underlying databaseExecute operations on underlying database
![Page 51: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/51.jpg)
Application creates an SQLite database and inserts records, some with errors, into itWhen user presses the Fix button, the application deletes, updates and redisplays the corrected database records
![Page 52: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/52.jpg)
DataManagementSQL
![Page 53: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/53.jpg)
public class DatabaseExampleActivity extends ListActivity {private DatabaseOpenHelper mDbHelper;private SimpleCursorAdapter mAdapter;Cursor mCursor;
public void onCreate(Bundle savedInstanceState) {…
mDbHelper = new DatabaseOpenHelper(this);clearAll();insertArtists();mCursor = readArtists();mAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, mCursor,
DatabaseOpenHelper.columns, new int[] { R.id._id, R.id.name },0);
setListAdapter(mAdapter);
}
![Page 54: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/54.jpg)
// Delete all recordsprivate void clearAll() {
mDbHelper.getWritableDatabase().delete(DatabaseOpenHelper.TABLE_NAME, null, null);
}
![Page 55: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/55.jpg)
// Insert several artist recordsprivate void insertArtists() {
ContentValues values = new ContentValues();
values.put(DatabaseOpenHelper.ARTIST_NAME, "Frank Sinatra");mDbHelper.getWritableDatabase()
.insert(DatabaseOpenHelper.TABLE_NAME, null, values);…values.clear();
values.put(DatabaseOpenHelper.ARTIST_NAME, "Ludwig van Beethoven");mDbHelper.getWritableDatabase()
.insert(DatabaseOpenHelper.TABLE_NAME, null, values);}
![Page 56: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/56.jpg)
// Returns all artist records in the databaseprivate Cursor readArtists() {
return mDbHelper.getWritableDatabase().query(DatabaseOpenHelper.TABLE_NAME,
DatabaseOpenHelper.columns, null, new String[] {}, null, null, null);}
![Page 57: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/57.jpg)
public void onClick(View v) {
// Execute database operationsfix();
// Redisplay datamCursor = readArtists();mAdapter.changeCursor(mCursor);
}
![Page 58: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/58.jpg)
// Modify the contents of the databaseprivate void fix() {
// Sorry Lady Gaga :-(mDbHelper.getWritableDatabase()
.delete(DatabaseOpenHelper.TABLE_NAME,DatabaseOpenHelper.ARTIST_NAME + "=?",new String[] { "Lady Gaga" });
// fix the Man in BlackContentValues values = new ContentValues();values.put(DatabaseOpenHelper.ARTIST_NAME, "Johnny Cash");
mDbHelper.getWritableDatabase().update(DatabaseOpenHelper.TABLE_NAME, values,
DatabaseOpenHelper.ARTIST_NAME + "=?", new String[] { "Jawny Cash" });
}
![Page 59: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/59.jpg)
Databases stored in /data/data/<package name>/databases/
Can examine database with sqlite3# adb -s emulator-5554 shell# su# sqlite3 \/data/data/course.examples.datamanagement.sql/databases/artist_db
![Page 60: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/60.jpg)
Lifecycle-Aware Components
![Page 61: SharedPreferences Internal Storage External Storage€¦ · // Fragment that displays the username preference public static class UserPreferenceFragmentextends PreferenceFragment{](https://reader034.vdocument.in/reader034/viewer/2022050420/5f8fd0496edae227a63db94e/html5/thumbnails/61.jpg)
DataManagementSharedPreferencesDataManagementPreferenceFragmentDataManagementFileInternalMemoryDataManagementFileExternalMemory
DataManagementSQL