Home | History | Annotate | Download | only in notepad1
      1 /*
      2  * Copyright (C) 2008 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.demo.notepad1;
     18 
     19 import android.app.ListActivity;
     20 import android.database.Cursor;
     21 import android.os.Bundle;
     22 import android.view.Menu;
     23 import android.view.MenuItem;
     24 import android.widget.SimpleCursorAdapter;
     25 
     26 public class Notepadv1 extends ListActivity {
     27 	public static final int INSERT_ID = Menu.FIRST;
     28 
     29 	private int mNoteNumber = 1;
     30 	private NotesDbAdapter mDbHelper;
     31 
     32     /** Called when the activity is first created. */
     33     @Override
     34     public void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.notepad_list);
     37         mDbHelper = new NotesDbAdapter(this);
     38         mDbHelper.open();
     39         fillData();
     40     }
     41 
     42     @Override
     43     public boolean onCreateOptionsMenu(Menu menu) {
     44     	boolean result = super.onCreateOptionsMenu(menu);
     45         menu.add(0, INSERT_ID, 0, R.string.menu_insert);
     46         return result;
     47     }
     48 
     49     @Override
     50     public boolean onOptionsItemSelected(MenuItem item) {
     51     	switch (item.getItemId()) {
     52         case INSERT_ID:
     53             createNote();
     54             return true;
     55         }
     56         return super.onOptionsItemSelected(item);
     57     }
     58 
     59     private void createNote() {
     60         String noteName = "Note " + mNoteNumber++;
     61         mDbHelper.createNote(noteName, "");
     62         fillData();
     63     }
     64 
     65     private void fillData() {
     66         // Get all of the notes from the database and create the item list
     67         Cursor c = mDbHelper.fetchAllNotes();
     68         startManagingCursor(c);
     69 
     70         String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
     71         int[] to = new int[] { R.id.text1 };
     72 
     73         // Now create an array adapter and set it to display using our row
     74         SimpleCursorAdapter notes =
     75             new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
     76         setListAdapter(notes);
     77     }
     78 }
     79