Ch02 Listings
satya - Friday, February 26, 2010 1:48:49 PM
Listing 2-1.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.notepad"
>
   <application android:icon="@drawable/app_notes"
        android:label="@string/app_name"
    >
        <provider android:name="NotePadProvider"
            android:authorities="com.google.provider.NotePad"
        />
        <activity android:name="NotesList" android:label="@string/title_notes_list">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
            </intent-filter>
        </activity>
      </application>
</manfiest>
satya - Friday, February 26, 2010 1:50:05 PM
Listing 2-2.java
public class NotesList extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
        Intent intent = getIntent();
        if (intent.getData() == null) {
            intent.setData(Notes.CONTENT_URI);
        }
        getListView().setOnCreateContextMenuListener(this);
        
        Cursor cursor = managedQuery(getIntent().getData(), 
                PROJECTION, null, null,
                Notes.DEFAULT_SORT_ORDER);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
              R.layout.noteslist_item, cursor, new String[] { Notes.TITLE }, 
              new int[] { android.R.id.text1 });
        setListAdapter(adapter);
}
}
satya - Friday, February 26, 2010 1:51:13 PM
Listing 2-3.java
@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
        
        String action = getIntent().getAction();
        if (Intent.ACTION_PICK.equals(action) || 
            Intent.ACTION_GET_CONTENT.equals(action)) 
        {
            setResult(RESULT_OK, new Intent().setData(uri));
         } 
        else 
        {
            startActivity(new Intent(Intent.ACTION_EDIT, uri));
        }
    }
satya - Friday, February 26, 2010 1:53:17 PM
Listing 2-4.java
public class NotePadProvider extends ContentProvider
{
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                         String[] selectionArgs,String sortOrder) {}
    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {}
    @Override
    public int update(Uri uri, ContentValues values, String where, 
                       String[] whereArgs) {}
    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {}
    @Override
    public String getType(Uri uri) {}
    @Override
    public boolean onCreate() {}
    private static class DatabaseHelper extends SQLiteOpenHelper {}
    @Override
        public void onCreate(SQLiteDatabase db) {}
        @Override
        public void onUpgrade(SQLiteDatabase db, 
                   int oldVersion, int newVersion) {
            //...
        }
    }
}
satya - Friday, February 26, 2010 1:53:57 PM
Listing 2-5.java
private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + NOTES_TABLE_NAME + " ("
                    + Notes._ID + " INTEGER PRIMARY KEY,"
                    + Notes.TITLE + " TEXT,"
                    + Notes.NOTE + " TEXT,"
                    + Notes.CREATED_DATE + " INTEGER,"
                    + Notes.MODIFIED_DATE + " INTEGER"
                    + ");");
        }
}
satya - Friday, February 26, 2010 1:54:44 PM
Listing 2-6.java
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long rowId = db.insert(NOTES_TABLE_NAME, Notes.NOTE, values);
if (rowId > 0) {
	Uri noteUri = ContentUris.withAppendedId(
						 NotePad.Notes.CONTENT_URI, rowId);
	getContext().getContentResolver().notifyChange(noteUri, null);
	return noteUri;
}
satya - Friday, February 26, 2010 1:55:11 PM
Listing 2-7.java
protected void onCreate(Bundle savedInstanceState); 
protected void onStart(); 
protected void onRestart(); 
protected void onResume(); 
protected void onPause(); 
protected void onStop(); 
protected void onDestroy();
satya - Friday, February 26, 2010 1:55:33 PM
Listing 2-8.java
public class MyApplication extends Application 
{
    // global variable
    private static final String myGlobalVariable;
    @Override
    public void onCreate() 
    {
        super.onCreate();
        //... initialize global variables here
        myGlobalVariable = loadCacheData();
    }
    public static String getMyGlobalVariable() {
        return myGlobalVariable;
    }
}
