Skip to content

Instantly share code, notes, and snippets.

/** For this example, assume that
* public static final int GENDER_MALE = 1;
* public static final int GENDER_FEMALE = 2;
**/
String[] projection = { PetEntry.COLUMN_PET_BREED,
PetEntry.COLUMN_PET_WEIGHT };
String selection = PetEntry.COLUMN_PET_GENDER + “=?”;
String[] selectionArgs = new String[] { String.valueOf(PetEntry.GENDERFEMALE) };
@udacityandroid
udacityandroid / Excerpt from PetProvider.java
Last active November 13, 2019 20:46
Pets app - Replace delete() method in PetProvider
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Get writeable database
SQLiteDatabase database = mDbHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
switch (match) {
case PETS:
// Delete all rows that match the selection and selection args
return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
@udacityandroid
udacityandroid / StoreContract.java
Created July 26, 2016 22:13
Example contract class for a table which contains data about the store's headphones stock
public final class StoreContract {
public static abstract class HeadphoneEntry implements BaseColumns {
public static final String TABLE_NAME = "headphones";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_PRICE = "price";
public static final String COLUMN_STYLE = "style";
public static final String COLUMN_IN_STOCK = "in_stock";
public static final String COLUMN_DESCRIPTION = "description";
@udacityandroid
udacityandroid / WeatherContract.java
Created July 26, 2016 21:54
Contract class for a weather application called Sunshine
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@udacityandroid
udacityandroid / onCreateView() method in NumbersFragment.java
Created May 14, 2016 00:47
After fixing 4 errors in NumbersFragment.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// Create and setup the {@link AudioManager} to request audio focus
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Create a list of words
final ArrayList<Word> words = new ArrayList<Word>();
@udacityandroid
udacityandroid / Code for releaseMediaPlayer from NumbersActivity.java
Created May 14, 2016 00:36
Copy over the releaseMediaPlayer() helper method from the NumbersActivity into the NumbersFragment.
/**
* Clean up the media player by releasing its resources.
*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mMediaPlayer != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
mMediaPlayer.release();
@udacityandroid
udacityandroid / Code to go into NumberFragment.java
Created May 14, 2016 00:34
Copy code from the NumbersActivity and paste it into the NumbersFragment
/** Handles playback of all the sound files */
private MediaPlayer mMediaPlayer;
/** Handles audio focus when playing a sound file */
private AudioManager mAudioManager;
/**
* This listener gets triggered whenever the audio focus changes
* (i.e., we gain or lose audio focus because of another app or device).
*/
@udacityandroid
udacityandroid / list_item.xml
Created May 13, 2016 21:08
Miwok App: Miwok app: List Item Views with pressed states
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:background="@color/tan_background"
android:orientation="horizontal">
@udacityandroid
udacityandroid / activity_main.xml
Created May 13, 2016 19:14
Miwok app: Category screen with pressed states
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">
<!-- Numbers category -->
@udacityandroid
udacityandroid / Helper Method - releaseMediaPlayer()
Last active May 19, 2023 05:51
Miwok app: Cleaning up MediaPlayer resources
/**
* Clean up the media player by releasing its resources.
*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mMediaPlayer != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
mMediaPlayer.release();