| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Android Notepad チュートリアル」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
 
1行目: 1行目:
==Android Notepad チュートリアル==
+
==[[Android Notepad チュートリアル]]==
 
[[Android]] | [[Java]] | [[Eclipse]] |  
 
[[Android]] | [[Java]] | [[Eclipse]] |  
  
9行目: 9行目:
 
==(1) 簡単なノートリストの作成==
 
==(1) 簡単なノートリストの作成==
 
*新しいノートを追加する(まだ編集はできない)
 
*新しいノートを追加する(まだ編集はできない)
*ListActivity を使ってみる
+
*ListActi[[vi]]ty を使ってみる
*SQLiteを使ってみる
+
*[[SQL]]iteを使ってみる
  
 
===プロジェクトの作成===
 
===プロジェクトの作成===
20行目: 20行目:
 
===NotesDbAdapter===
 
===NotesDbAdapter===
 
====用途====
 
====用途====
*NotesDbAdapterはSQLiteへのデータアクセスをカプセル化する。
+
*NotesDbAdapterは[[SQL]]iteへのデータアクセスをカプセル化する。
 
====宣言====
 
====宣言====
 
*クラスの先頭で、テーブルのフィールドおよびデータベースが存在しない場合に生成する文字列を定義
 
*クラスの先頭で、テーブルのフィールドおよびデータベースが存在しない場合に生成する文字列を定義
 
*データベースは data という名前で、notes という _id、title、bodyの3つのフィールドをもつテーブルを一つ持つ。
 
*データベースは data という名前で、notes という _id、title、bodyの3つのフィールドをもつテーブルを一つ持つ。
 
====コンストラクタ====
 
====コンストラクタ====
*コンストラクタは、Context パラメータを取り、それにより Android OS との通信を許可。これは Android にアクセスする一般的な方法
+
*コンストラクタは、Context パラメータを取り、それにより [[Android]] OS との通信を許可。これは [[Android]] にアクセスする一般的な方法
*Activity クラスは Context を実装しているため、Contextが必要な時に渡すことができる。
+
*Acti[[vi]]ty クラスは Context を実装しているため、Contextが必要な時に渡すことができる。
  
 
====メソッド====
 
====メソッド====
 
=====open()=====
 
=====open()=====
*open()メソッドは、DatabaseHelper のインスタンスを呼び出す。
+
*open()メソッドは、[[Database]]Helper のインスタンスを呼び出す。
*DatabaseHelper は SQLiteOpenHelper のローカル実装。
+
*[[Database]]Helper は SQLiteOpenHelper のローカル実装。
*getWritableDatabase() で、データベースを作成、オープン
+
*getWritable[[Database]]() で、データベースを作成、オープン
  
 
=====close() =====
 
=====close() =====
46行目: 46行目:
 
=====fetchAllNotes()=====
 
=====fetchAllNotes()=====
 
*クエリーを発行して、カーソルを得る
 
*クエリーを発行して、カーソルを得る
*query() には、データベーステーブル名、取得したいカラムリスト、残りのフィールドは順に、selection、selectionArgs、 groupBy、 having、orderBy
+
*query() には、データベーステーブル名、取得したいカラムリスト、残りのフィールドは順に、selection、selectionArgs、 groupBy、 ha[[vi]]ng、orderBy
 
*fetchNote()は、fetchAllNotes() と同様だが、行IDで1つの note を取得する
 
*fetchNote()は、fetchAllNotes() と同様だが、行IDで1つの note を取得する
  
55行目: 55行目:
 
====LinearLayout の設定====
 
====LinearLayout の設定====
  
=====すべての Android のXMLレイアウトのヘッダーは以下で始まらなければいけない=====
+
=====すべての [[Android]] のXMLレイアウトのヘッダーは以下で始まらなければいけない=====
 
  <?xml version="1.0" encoding="utf-8"?>.
 
  <?xml version="1.0" encoding="utf-8"?>.
 
=====次に、たいていの場合、以下の様なレイアウトを置く=====
 
=====次に、たいていの場合、以下の様なレイアウトを置く=====
 
  LinearLayout
 
  LinearLayout
=====トップレベルのコンポーネントまたはレイアウトで Android のネームスペースを宣言する=====
+
=====トップレベルのコンポーネントまたはレイアウトで [[Android]] のネームスペースを宣言する=====
 
  xmlns:android="http://schemas.android.com/apk/res/android"
 
  xmlns:android="http://schemas.android.com/apk/res/android"
 
===LinearLayout ===
 
===LinearLayout ===
86行目: 86行目:
 
====オーバーライドメソッド====
 
====オーバーライドメソッド====
 
=====onCreate() =====
 
=====onCreate() =====
*Activityが開始されたときに呼び出される。
+
*Acti[[vi]]tyが開始されたときに呼び出される。
 
*リソースの設定、ステートの設定を実施
 
*リソースの設定、ステートの設定を実施
=====onCreateOptionsMenu() =====
+
=====onCreateOptions[[Menu]]() =====
*Activity メニューの有効化
+
*Acti[[vi]]ty メニューの有効化
 
*ユーザーがメニューボタンをたたいた時に呼び出される
 
*ユーザーがメニューボタンをたたいた時に呼び出される
 
=====onOptionsItemSelected() =====
 
=====onOptionsItemSelected() =====
95行目: 95行目:
 
*メニューから生成されるイベントを制御
 
*メニューから生成されるイベントを制御
 
====基底クラスの変更====
 
====基底クラスの変更====
*Activity ListActivity
+
*Acti[[vi]]ty ListActi[[vi]]ty
  public class Notepadv1 extends ListActivity
+
  public class Notepadv1 extends ListActi[[vi]]ty
  
 
====onCreate()====
 
====onCreate()====
  private NotesDbAdapter mDbHelper;
+
  private NotesDbAdapter mDb[[Help]]er;
 
   
 
   
  /** Called when the activity is first created. */
+
  /** Called when the acti[[vi]]ty is first created. */
 
  @Override
 
  @Override
 
  public void onCreate(Bundle savedInstanceState) {
 
  public void onCreate(Bundle savedInstanceState) {
 
     super.onCreate(savedInstanceState);
 
     super.onCreate(savedInstanceState);
     setContentView(R.layout.notepad_list);
+
     setContentView([[R]].layout.notepad_list);
     mDbHelper = new NotesDbAdapter(this);
+
     mDb[[Help]]er = new NotesDbAdapter(this);
     mDbHelper.open();
+
     mDb[[Help]]er.open();
 
     fillData();
 
     fillData();
 
  }
 
  }
  
 
*fillData() はまだ未作成
 
*fillData() はまだ未作成
====onCreateOptionsMenu()====
+
====onCreateOptions[[Menu]]()====
 
  <?xml version="1.0" encoding="utf-8"?>
 
  <?xml version="1.0" encoding="utf-8"?>
 
  <resources>
 
  <resources>
121行目: 121行目:
  
  
  public class Notepadv1 extends ListActivity {
+
  public class Notepadv1 extends ListActi[[vi]]ty {
     public static final int INSERT_ID = Menu.FIRST;
+
     public static final int INSERT_ID = [[Menu]].FIRST;
 
       :   
 
       :   
  
 
  @Override
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
+
  public boolean onCreateOptions[[Menu]]([[Menu]] menu) {
 
     // TODO Auto-generated method stub
 
     // TODO Auto-generated method stub
     boolean result = super.onCreateOptionsMenu(menu);
+
     boolean result = super.onCreateOptions[[Menu]](menu);
     menu.add(0, INSERT_ID, 0, R.string.menu_insert);
+
     menu.add(0, INSE[[R]]T_ID, 0, [[R]].string.menu_insert);
 
     return result;
 
     return result;
 
  }
 
  }
135行目: 135行目:
 
====onOptionsItemSelected()====
 
====onOptionsItemSelected()====
 
  @Override
 
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
+
  public boolean onOptionsItemSelected([[Menu]]Item item) {
 
     // TODO Auto-generated method stub
 
     // TODO Auto-generated method stub
 
     switch (item.getItemId()) {
 
     switch (item.getItemId()) {
     case INSERT_ID:
+
     case INSE[[R]]T_ID:
 
     createNote();
 
     createNote();
 
         return true;
 
         return true;
148行目: 148行目:
 
  public void createNote() {
 
  public void createNote() {
 
     String noteName = "Note " + mNoteNumber++;
 
     String noteName = "Note " + mNoteNumber++;
     mDbHelper.createNote(noteName, "");
+
     mDb[[Help]]er.createNote(noteName, "");
 
     fillData();
 
     fillData();
 
  }
 
  }
 
====fillData()====
 
====fillData()====
 
  private void fillData() {
 
  private void fillData() {
     Cursor c = mDbHelper.fetchAllNotes();
+
     Cursor c = mDb[[Help]]er.fetchAllNotes();
 
     startManagingCursor(c);
 
     startManagingCursor(c);
 
    
 
    
160行目: 160行目:
 
    
 
    
 
     SimpleCursorAdapter notes =  
 
     SimpleCursorAdapter notes =  
         new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
+
         new SimpleCursorAdapter(this, [[R]].layout.notes_row, c, from, to);
 
     setListAdapter(notes);
 
     setListAdapter(notes);
 
  }
 
  }
166行目: 166行目:
 
===ここまでで、起動===
 
===ここまでで、起動===
 
[[File:0147_android_notepad02.jpg]]
 
[[File:0147_android_notepad02.jpg]]
==(2) Activity の追加、高度なレイアウトの使用など==
+
==(2) Acti[[vi]]ty の追加、高度なレイアウトの使用など==
 
*http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html
 
*http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html
*2つめのActivityを作成しマニフェストに追加
+
*2つめのActi[[vi]]tyを作成しマニフェストに追加
*startActivityForResult()を使って非同期で別のActivityを呼び出す
+
*startActi[[vi]]tyForResult()を使って非同期で別のActi[[vi]]tyを呼び出す
*Bundle オブジェクトで Activity 間でデータを受け渡す
+
*Bundle オブジェクトで Acti[[vi]]ty 間でデータを受け渡す
 
*他のさらに高度なレイアウトの使用
 
*他のさらに高度なレイアウトの使用
 
*コンテキストメニューの作成
 
*コンテキストメニューの作成
179行目: 179行目:
 
*fillData()では、mNotesCursor カーソルフィールドを使うようになっている。
 
*fillData()では、mNotesCursor カーソルフィールドを使うようになっている。
 
*onCreate()は変更なし。
 
*onCreate()は変更なし。
*いくつかのオーバーライドメソッドが記述されている(onCreateContextMenu(), onContextItemSelected(), onListItemClick() and onActivityResult())
+
*いくつかのオーバーライドメソッドが記述されている(onCreateContext[[Menu]](), onContextItemSelected(), onListItemClick() and onActivityResult())
  
 
===表示されている note を削除するためのコンテキストメニューを追加する===
 
===表示されている note を削除するためのコンテキストメニューを追加する===
====registerForContextMenu()====
+
====registerForContext[[Menu]]()====
*ListView のアイテムそれぞれに順にコンテキストメニューにregisterForContextMenu()を呼び出して登録
+
*ListView のアイテムそれぞれに順にコンテキストメニューにregisterForContext[[Menu]]()を呼び出して登録
 
*onCreate() の最後に以下を追加
 
*onCreate() の最後に以下を追加
 
  @Override
 
  @Override
 
  public void onCreate(Bundle savedInstanceState) {
 
  public void onCreate(Bundle savedInstanceState) {
 
     super.onCreate(savedInstanceState);
 
     super.onCreate(savedInstanceState);
     setContentView(R.layout.notes_list);
+
     setContentView([[R]].layout.notes_list);
     mDbHelper = new NotesDbAdapter(this);
+
     mDb[[Help]]er = new NotesDbAdapter(this);
     mDbHelper.open();
+
     mDb[[Help]]er.open();
 
     fillData();
 
     fillData();
 
      
 
      
     registerForContextMenu(getListView());
+
     registerForContext[[Menu]](getListView());
 
  }
 
  }
  
====onCreateContextMenu()====
+
====onCreateContext[[Menu]]()====
 
  @Override
 
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
+
  public void onCreateContext[[Menu]](Context[[Menu]] menu, View v,
         ContextMenuInfo menuInfo) {
+
         Context[[Menu]]Info menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
+
     super.onCreateContext[[Menu]](menu, v, menuInfo);
 
      
 
      
 
     // TODO: fill in rest of method
 
     // TODO: fill in rest of method
     super.onCreateContextMenu(menu, v, menuInfo);
+
     super.onCreateContext[[Menu]](menu, v, menuInfo);
     menu.add(0, DELETE_ID, 0, R.string.menu_delete);
+
     menu.add(0, DELETE_ID, 0, [[R]].string.menu_delete);
 
  }
 
  }
  
 
====onContextItemSelected()====
 
====onContextItemSelected()====
 
  @Override
 
  @Override
   public boolean onContextItemSelected(MenuItem item) {
+
   public boolean onContextItemSelected([[Menu]]Item item) {
 
        
 
        
 
     // TODO: fill in rest of method
 
     // TODO: fill in rest of method
 
       switch(item.getItemId()) {
 
       switch(item.getItemId()) {
 
       case DELETE_ID:
 
       case DELETE_ID:
             AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
+
             AdapterContext[[Menu]]Info info = (AdapterContext[[Menu]]Info) item.get[[Menu]]Info();
             mDbHelper.deleteNote(info.id);
+
             mDb[[Help]]er.deleteNote(info.id);
 
             fillData();
 
             fillData();
 
             return true;
 
             return true;
222行目: 222行目:
 
   }
 
   }
  
====onActivityResult()====
+
====onActi[[vi]]tyResult()====
 
  @Override
 
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
+
  protected void onActi[[vi]]tyResult(int requestCode, int resultCode, Intent intent) {
 
     // TODO Auto-generated method stub
 
     // TODO Auto-generated method stub
     super.onActivityResult(requestCode, resultCode, intent);
+
     super.onActi[[vi]]tyResult(requestCode, resultCode, intent);
 
     Bundle extras = intent.getExtras();
 
     Bundle extras = intent.getExtras();
 
      
 
      
 
     switch(requestCode) {
 
     switch(requestCode) {
     case ACTIVITY_CREATE:
+
     case ACTIVITY_C[[R]]EATE:
 
         String title = extras.getString(NotesDbAdapter.KEY_TITLE);
 
         String title = extras.getString(NotesDbAdapter.KEY_TITLE);
 
         String body = extras.getString(NotesDbAdapter.KEY_BODY);
 
         String body = extras.getString(NotesDbAdapter.KEY_BODY);
         mDbHelper.createNote(title, body);
+
         mDb[[Help]]er.createNote(title, body);
 
         fillData();
 
         fillData();
 
         break;
 
         break;
 
     case ACTIVITY_EDIT:
 
     case ACTIVITY_EDIT:
         Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
+
         Long m[[R]]owId = extras.getLong(NotesDbAdapter.KEY_[[R]]OWID);
         if (mRowId != null) {
+
         if (m[[R]]owId != null) {
 
             String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
 
             String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
 
             String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
 
             String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
             mDbHelper.updateNote(mRowId, editTitle, editBody);
+
             mDb[[Help]]er.updateNote(mRowId, editTitle, editBody);
 
         }
 
         }
 
         fillData();
 
         fillData();
251行目: 251行目:
 
     // TODO: fill in implementation
 
     // TODO: fill in implementation
 
     Intent i = new Intent(this, NoteEdit.class);
 
     Intent i = new Intent(this, NoteEdit.class);
     startActivityForResult(i, ACTIVITY_CREATE);
+
     startActi[[vi]]tyForResult(i, ACTIVITY_CREATE);
 
  }
 
  }
  
264行目: 264行目:
 
     c.moveToPosition(position);
 
     c.moveToPosition(position);
 
     Intent i = new Intent(this, NoteEdit.class);
 
     Intent i = new Intent(this, NoteEdit.class);
     i.putExtra(NotesDbAdapter.KEY_ROWID, id);
+
     i.putExtra(NotesDbAdapter.KEY_[[R]]OWID, id);
 
     i.putExtra(NotesDbAdapter.KEY_TITLE,
 
     i.putExtra(NotesDbAdapter.KEY_TITLE,
 
             c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
 
             c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
270行目: 270行目:
 
             c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
 
             c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
 
      
 
      
     startActivityForResult(i, ACTIVITY_EDIT);
+
     startActi[[vi]]tyForResult(i, ACTIVITY_EDIT);
 
  }
 
  }
  
====onActivityResult()====
+
====onActi[[vi]]tyResult()====
 
  @Override
 
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
+
  protected void onActi[[vi]]tyResult(int requestCode, int resultCode, Intent intent) {
     super.onActivityResult(requestCode, resultCode, intent);
+
     super.onActi[[vi]]tyResult(requestCode, resultCode, intent);
 
      
 
      
 
     // TODO: fill in rest of method
 
     // TODO: fill in rest of method
282行目: 282行目:
 
      
 
      
 
     switch(requestCode) {
 
     switch(requestCode) {
     case ACTIVITY_CREATE:
+
     case ACTIVITY_C[[R]]EATE:
 
         String title = extras.getString(NotesDbAdapter.KEY_TITLE);
 
         String title = extras.getString(NotesDbAdapter.KEY_TITLE);
 
         String body = extras.getString(NotesDbAdapter.KEY_BODY);
 
         String body = extras.getString(NotesDbAdapter.KEY_BODY);
         mDbHelper.createNote(title, body);
+
         mDb[[Help]]er.createNote(title, body);
 
         fillData();
 
         fillData();
 
         break;
 
         break;
 
     case ACTIVITY_EDIT:
 
     case ACTIVITY_EDIT:
         Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
+
         Long m[[R]]owId = extras.getLong(NotesDbAdapter.KEY_[[R]]OWID);
         if (mRowId != null) {
+
         if (m[[R]]owId != null) {
 
             String editTitle  = extras.getString(NotesDbAdapter.KEY_TITLE);
 
             String editTitle  = extras.getString(NotesDbAdapter.KEY_TITLE);
 
             String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
 
             String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
             mDbHelper.updateNote(mRowId, editTitle, editBody);
+
             mDb[[Help]]er.updateNote(mRowId, editTitle, editBody);
 
         }
 
         }
 
         fillData();
 
         fillData();
300行目: 300行目:
 
  }
 
  }
 
===NoteEdit クラスの追加===
 
===NoteEdit クラスの追加===
  public class NoteEdit extends Activity {
+
  public class NoteEdit extends Acti[[vi]]ty {
 
     private EditText mTitleText;
 
     private EditText mTitleText;
 
     private EditText mBodyText;
 
     private EditText mBodyText;
     private Long mRowId;
+
     private Long m[[R]]owId;
 
      
 
      
 
     @Override
 
     @Override
309行目: 309行目:
 
         // TODO Auto-generated method stub
 
         // TODO Auto-generated method stub
 
         super.onCreate(savedInstanceState);
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.note_edit);
+
         setContentView([[R]].layout.note_edit);
         mTitleText = (EditText) findViewById(R.id.title);
+
         mTitleText = (EditText) findViewById([[R]].id.title);
         mBodyText = (EditText) findViewById(R.id.body);
+
         mBodyText = (EditText) findViewById([[R]].id.body);
         Button confirmButton = (Button) findViewById(R.id.confirm);
+
         Button confirmButton = (Button) findViewById([[R]].id.confirm);
 
          
 
          
 
         confirmButton.setOnClickListener(
 
         confirmButton.setOnClickListener(
321行目: 321行目:
 
                         bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
 
                         bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
 
                         bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
 
                         bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
                         if (mRowId != null) {
+
                         if (m[[R]]owId != null) {
                             bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
+
                             bundle.putLong(NotesDbAdapter.KEY_[[R]]OWID, m[[R]]owId);
 
                         }
 
                         }
 
                         Intent mIntent = new Intent();
 
                         Intent mIntent = new Intent();
 
                         mIntent.putExtras(bundle);
 
                         mIntent.putExtras(bundle);
                         setResult(RESULT_OK, mIntent);
+
                         set[[R]]esult([[R]]ESULT_OK, mIntent);
 
                         finish();
 
                         finish();
 
                     }
 
                     }
332行目: 332行目:
 
         );
 
         );
 
          
 
          
         mRowId = null;
+
         m[[R]]owId = null;
 
         Bundle extras = getIntent().getExtras();
 
         Bundle extras = getIntent().getExtras();
 
         if (extras != null) {
 
         if (extras != null) {
 
             String title = extras.getString(NotesDbAdapter.KEY_TITLE);
 
             String title = extras.getString(NotesDbAdapter.KEY_TITLE);
 
             String body = extras.getString(NotesDbAdapter.KEY_BODY);
 
             String body = extras.getString(NotesDbAdapter.KEY_BODY);
             mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
+
             m[[R]]owId = extras.getLong(NotesDbAdapter.KEY_[[R]]OWID);
 
              
 
              
 
             if (title != null) {
 
             if (title != null) {
349行目: 349行目:
 
  }
 
  }
  
===AndroidManifest.xml の編集===
+
===[[Android]]Manifest.xml の編集===
*<activity android:name=".NoteEdit"></activity>を追加
+
*<acti[[vi]]ty android:name=".NoteEdit"></acti[[vi]]ty>を追加
  
  <activity android:name=".Notepadv2" android:label="@string/app_name">
+
  <acti[[vi]]ty android:name=".Notepadv2" android:label="@string/app_name">
 
   :
 
   :
  </activity>
+
  </acti[[vi]]ty>
  <activity android:name=".NoteEdit"></activity>
+
  <acti[[vi]]ty android:name=".NoteEdit"></acti[[vi]]ty>
  
 
===ここまでで起動===
 
===ここまでで起動===
372行目: 372行目:
 
====onCreate()====
 
====onCreate()====
 
*メンバ変数の宣言
 
*メンバ変数の宣言
  private NotesDbAdapter mDbHelper;  
+
  private NotesDbAdapter mDb[[Help]]er;  
  
 
  @Override
 
  @Override
 
  protected void onCreate(Bundle savedInstanceState) {
 
  protected void onCreate(Bundle savedInstanceState) {
 
     super.onCreate(savedInstanceState);
 
     super.onCreate(savedInstanceState);
     mDbHelper = new NotesDbAdapter(this);
+
     mDb[[Help]]er = new NotesDbAdapter(this);
     mDbHelper.open();
+
     mDb[[Help]]er.open();
 
      
 
      
     setContentView(R.layout.note_edit);
+
     setContentView([[R]].layout.note_edit);
 
      
 
      
     mTitleText = (EditText) findViewById(R.id.title);
+
     mTitleText = (EditText) findViewById([[R]].id.title);
     mBodyText = (EditText) findViewById(R.id.body);
+
     mBodyText = (EditText) findViewById([[R]].id.body);
 
    
 
    
     Button confirmButton = (Button) findViewById(R.id.confirm);
+
     Button confirmButton = (Button) findViewById([[R]].id.confirm);
 
      
 
      
     mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID)
+
     m[[R]]owId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_[[R]]OWID)
 
                                         : null;
 
                                         : null;
     if (mRowId == null) {
+
     if (m[[R]]owId == null) {
 
         Bundle extras = getIntent().getExtras();
 
         Bundle extras = getIntent().getExtras();
         mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
+
         m[[R]]owId = extras != null ? extras.getLong(NotesDbAdapter.KEY_[[R]]OWID)
 
                                 : null;
 
                                 : null;
 
     }
 
     }
398行目: 398行目:
 
     confirmButton.setOnClickListener(new View.OnClickListener() {
 
     confirmButton.setOnClickListener(new View.OnClickListener() {
 
   
 
   
         public void onClick(View view) {
+
         public void onClick(View [[vi]]ew) {
             setResult(RESULT_OK);
+
             set[[R]]esult([[R]]ESULT_OK);
 
             finish();
 
             finish();
 
         }
 
         }
409行目: 409行目:
  
 
  private void populateFields() {
 
  private void populateFields() {
     if (mRowId != null) {
+
     if (m[[R]]owId != null) {
         Cursor note = mDbHelper.fetchNote(mRowId);
+
         Cursor note = mDb[[Help]]er.fetchNote(mRowId);
 
     startManagingCursor(note);
 
     startManagingCursor(note);
 
     mTitleText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
 
     mTitleText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
423行目: 423行目:
 
     // TODO Auto-generated method stub
 
     // TODO Auto-generated method stub
 
     super.onSaveInstanceState(outState);
 
     super.onSaveInstanceState(outState);
     outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
+
     outState.putLong(NotesDbAdapter.KEY_[[R]]OWID, m[[R]]owId);
 
  }
 
  }
 
=====onPause()=====
 
=====onPause()=====
433行目: 433行目:
 
     saveState();
 
     saveState();
 
  }
 
  }
=====onResume()=====
+
=====on[[R]]esume()=====
 
  @Override
 
  @Override
  protected void onResume() {
+
  protected void on[[R]]esume() {
 
     // TODO Auto-generated method stub
 
     // TODO Auto-generated method stub
     super.onResume();
+
     super.on[[R]]esume();
 
     populateFields();
 
     populateFields();
 
  }
 
  }
446行目: 446行目:
 
     String body = mBodyText.getText().toString();
 
     String body = mBodyText.getText().toString();
 
      
 
      
     if (mRowId == null) {
+
     if (m[[R]]owId == null) {
         long id = mDbHelper.createNote(title, body);
+
         long id = mDb[[Help]]er.createNote(title, body);
 
         if (id > 0) {
 
         if (id > 0) {
             mRowId = id;
+
             m[[R]]owId = id;
 
         }
 
         }
 
     } else {
 
     } else {
         mDbHelper.updateNote(mRowId, title, body);
+
         mDb[[Help]]er.updateNote(mRowId, title, body);
 
     }
 
     }
 
  }
 
  }
  
 
===Notepadv3 ===
 
===Notepadv3 ===
====onActivityResult()====
+
====onActi[[vi]]tyResult()====
 
  @Override
 
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
+
  protected void onActi[[vi]]tyResult(int requestCode, int resultCode, Intent intent) {
     super.onActivityResult(requestCode, resultCode, intent);
+
     super.onActi[[vi]]tyResult(requestCode, resultCode, intent);
 
     fillData();
 
     fillData();
 
  }
 
  }
469行目: 469行目:
 
   
 
   
 
     Intent i = new Intent(this, NoteEdit.class);
 
     Intent i = new Intent(this, NoteEdit.class);
     i.putExtra(NotesDbAdapter.KEY_ROWID, id);
+
     i.putExtra(NotesDbAdapter.KEY_[[R]]OWID, id);
     startActivityForResult(i, ACTIVITY_EDIT);
+
     startActi[[vi]]tyForResult(i, ACTIVITY_EDIT);
 
  }
 
  }

2020年2月16日 (日) 04:21時点における最新版

目次

Android Notepad チュートリアル

Android | Java | Eclipse |

準備

ダウンロードして展開

(1) 簡単なノートリストの作成

  • 新しいノートを追加する(まだ編集はできない)
  • ListActivity を使ってみる
  • SQLiteを使ってみる

プロジェクトの作成

  • Create project from existing source を選択
  • 解凍したフォルダから、NotepadCodeLab/Notepadv1 を選択
  • Finish

0146 android notepad01.jpg

NotesDbAdapter

用途

  • NotesDbAdapterはSQLiteへのデータアクセスをカプセル化する。

宣言

  • クラスの先頭で、テーブルのフィールドおよびデータベースが存在しない場合に生成する文字列を定義
  • データベースは data という名前で、notes という _id、title、bodyの3つのフィールドをもつテーブルを一つ持つ。

コンストラクタ

  • コンストラクタは、Context パラメータを取り、それにより Android OS との通信を許可。これは Android にアクセスする一般的な方法
  • Activity クラスは Context を実装しているため、Contextが必要な時に渡すことができる。

メソッド

open()
  • open()メソッドは、DatabaseHelper のインスタンスを呼び出す。
  • DatabaseHelper は SQLiteOpenHelper のローカル実装。
  • getWritableDatabase() で、データベースを作成、オープン
close()
  • データベースを閉じてリリースを解放
createNote()
  • 新しい note のためのタイトルと本文の文字列を引数に取り、note をデータベースに作成する
  • 作成が成功した場合、_id を返す。
deleteNote()
  • 行ID を引数に取り、note をデータベースから削除
fetchAllNotes()
  • クエリーを発行して、カーソルを得る
  • query() には、データベーステーブル名、取得したいカラムリスト、残りのフィールドは順に、selection、selectionArgs、 groupBy、 having、orderBy
  • fetchNote()は、fetchAllNotes() と同様だが、行IDで1つの note を取得する
updateNote()
  • 行ID、タイトル、本文を引数に取り、ContentValuesを使ってnoteを更新する。

レイアウト

LinearLayout の設定

すべての Android のXMLレイアウトのヘッダーは以下で始まらなければいけない
  1. <?xml version="1.0" encoding="utf-8"?>.
次に、たいていの場合、以下の様なレイアウトを置く
  1. LinearLayout
トップレベルのコンポーネントまたはレイアウトで Android のネームスペースを宣言する
  1. xmlns:android="http://schemas.android.com/apk/res/android"

LinearLayout

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content">
  5. <ListView android:id="@android:id/list"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"/>
  8. <TextView android:id="@android:id/empty"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="@string/no_notes"/>
  12. </LinearLayout>

行のレイアウト

notes_row.xml

  • res/layout に notes_row.xml を作成
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView android:id="@+id/text1"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"/>

Notepadv1 クラスの編集

オーバーライドメソッド

onCreate()
  • Activityが開始されたときに呼び出される。
  • リソースの設定、ステートの設定を実施
onCreateOptionsMenu()
  • Activity メニューの有効化
  • ユーザーがメニューボタンをたたいた時に呼び出される
onOptionsItemSelected()
  • 他の半数のメニューと等しい
  • メニューから生成されるイベントを制御

基底クラスの変更

  1. public class Notepadv1 extends ListActivity

onCreate()

  1. private NotesDbAdapter mDbHelper;
  2.  
  3. /** Called when the activity is first created. */
  4. @Override
  5. public void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.notepad_list);
  8. mDbHelper = new NotesDbAdapter(this);
  9. mDbHelper.open();
  10. fillData();
  11. }
  • fillData() はまだ未作成

onCreateOptionsMenu()

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">Notepad v1</string>
  4. <string name="no_notes">No Notes Yet</string>
  5. <string name="menu_insert">Add Item</string>
  6. </resources>


  1. public class Notepadv1 extends ListActivity {
  2. public static final int INSERT_ID = Menu.FIRST;
  1. @Override
  2. public boolean onCreateOptionsMenu(Menu menu) {
  3. // TODO Auto-generated method stub
  4. boolean result = super.onCreateOptionsMenu(menu);
  5. menu.add(0, INSERT_ID, 0, R.string.menu_insert);
  6. return result;
  7. }

onOptionsItemSelected()

  1. @Override
  2. public boolean onOptionsItemSelected(MenuItem item) {
  3. // TODO Auto-generated method stub
  4. switch (item.getItemId()) {
  5. case INSERT_ID:
  6. createNote();
  7. return true;
  8. }
  9. return super.onOptionsItemSelected(item);
  10. }

createNote()

  1. public void createNote() {
  2. String noteName = "Note " + mNoteNumber++;
  3. mDbHelper.createNote(noteName, "");
  4. fillData();
  5. }

fillData()

  1. private void fillData() {
  2. Cursor c = mDbHelper.fetchAllNotes();
  3. startManagingCursor(c);
  4. String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
  5. int[] to = new int[] { R.id.text1 };
  6. SimpleCursorAdapter notes =
  7. new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
  8. setListAdapter(notes);
  9. }

ここまでで、起動

0147 android notepad02.jpg

(2) Activity の追加、高度なレイアウトの使用など

プロジェクトの作成

  • 上記 Notepadv1 同様に、NotepadCodeLab/Notepadv2 からプロジェクトを作成する。
  • /res/values/strings.xml に今回追加する機能の文字列が追加されている。
  • Notepadv2 クラス には、カーソルを使うための mNotesCursor フィールドとともに、いくつかの定数が定義されている。
  • fillData()では、mNotesCursor カーソルフィールドを使うようになっている。
  • onCreate()は変更なし。
  • いくつかのオーバーライドメソッドが記述されている(onCreateContextMenu(), onContextItemSelected(), onListItemClick() and onActivityResult())

表示されている note を削除するためのコンテキストメニューを追加する

registerForContextMenu()

  • ListView のアイテムそれぞれに順にコンテキストメニューにregisterForContextMenu()を呼び出して登録
  • onCreate() の最後に以下を追加
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.notes_list);
  5. mDbHelper = new NotesDbAdapter(this);
  6. mDbHelper.open();
  7. fillData();
  8. registerForContextMenu(getListView());
  9. }

onCreateContextMenu()

  1. @Override
  2. public void onCreateContextMenu(ContextMenu menu, View v,
  3. ContextMenuInfo menuInfo) {
  4. super.onCreateContextMenu(menu, v, menuInfo);
  5. // TODO: fill in rest of method
  6. super.onCreateContextMenu(menu, v, menuInfo);
  7. menu.add(0, DELETE_ID, 0, R.string.menu_delete);
  8. }

onContextItemSelected()

  1. @Override
  2. public boolean onContextItemSelected(MenuItem item) {
  3. // TODO: fill in rest of method
  4. switch(item.getItemId()) {
  5. case DELETE_ID:
  6. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  7. mDbHelper.deleteNote(info.id);
  8. fillData();
  9. return true;
  10. }
  11. return super.onContextItemSelected(item);
  12. }

onActivityResult()

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  3. // TODO Auto-generated method stub
  4. super.onActivityResult(requestCode, resultCode, intent);
  5. Bundle extras = intent.getExtras();
  6. switch(requestCode) {
  7. case ACTIVITY_CREATE:
  8. String title = extras.getString(NotesDbAdapter.KEY_TITLE);
  9. String body = extras.getString(NotesDbAdapter.KEY_BODY);
  10. mDbHelper.createNote(title, body);
  11. fillData();
  12. break;
  13. case ACTIVITY_EDIT:
  14. Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
  15. if (mRowId != null) {
  16. String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
  17. String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
  18. mDbHelper.updateNote(mRowId, editTitle, editBody);
  19. }
  20. fillData();
  21. break;
  22. }
  23. }

createNote()

  1. private void createNote() {
  2. // TODO: fill in implementation
  3. Intent i = new Intent(this, NoteEdit.class);
  4. startActivityForResult(i, ACTIVITY_CREATE);
  5. }

onListItemClick()

  1. @Override
  2. protected void onListItemClick(ListView l, View v, int position, long id) {
  3. super.onListItemClick(l, v, position, id);
  4.  
  5. // TODO: fill in rest of method
  6. Cursor c = mNotesCursor;
  7. c.moveToPosition(position);
  8. Intent i = new Intent(this, NoteEdit.class);
  9. i.putExtra(NotesDbAdapter.KEY_ROWID, id);
  10. i.putExtra(NotesDbAdapter.KEY_TITLE,
  11. c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
  12. i.putExtra(NotesDbAdapter.KEY_BODY,
  13. c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
  14. startActivityForResult(i, ACTIVITY_EDIT);
  15. }

onActivityResult()

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  3. super.onActivityResult(requestCode, resultCode, intent);
  4. // TODO: fill in rest of method
  5. Bundle extras = intent.getExtras();
  6. switch(requestCode) {
  7. case ACTIVITY_CREATE:
  8. String title = extras.getString(NotesDbAdapter.KEY_TITLE);
  9. String body = extras.getString(NotesDbAdapter.KEY_BODY);
  10. mDbHelper.createNote(title, body);
  11. fillData();
  12. break;
  13. case ACTIVITY_EDIT:
  14. Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
  15. if (mRowId != null) {
  16. String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
  17. String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
  18. mDbHelper.updateNote(mRowId, editTitle, editBody);
  19. }
  20. fillData();
  21. break;
  22. }
  23. }

NoteEdit クラスの追加

  1. public class NoteEdit extends Activity {
  2. private EditText mTitleText;
  3. private EditText mBodyText;
  4. private Long mRowId;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. // TODO Auto-generated method stub
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.note_edit);
  10. mTitleText = (EditText) findViewById(R.id.title);
  11. mBodyText = (EditText) findViewById(R.id.body);
  12. Button confirmButton = (Button) findViewById(R.id.confirm);
  13. confirmButton.setOnClickListener(
  14. new View.OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. Bundle bundle = new Bundle();
  18. bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
  19. bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
  20. if (mRowId != null) {
  21. bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
  22. }
  23. Intent mIntent = new Intent();
  24. mIntent.putExtras(bundle);
  25. setResult(RESULT_OK, mIntent);
  26. finish();
  27. }
  28. }
  29. );
  30. mRowId = null;
  31. Bundle extras = getIntent().getExtras();
  32. if (extras != null) {
  33. String title = extras.getString(NotesDbAdapter.KEY_TITLE);
  34. String body = extras.getString(NotesDbAdapter.KEY_BODY);
  35. mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
  36. if (title != null) {
  37. mTitleText.setText(title);
  38. }
  39. if (body != null) {
  40. mBodyText.setText(body);
  41. }
  42. }
  43. }
  44. }

AndroidManifest.xml の編集

  • <activity android:name=".NoteEdit"></activity>を追加
  1. <activity android:name=".Notepadv2" android:label="@string/app_name">
  2. :
  3. </activity>
  4. <activity android:name=".NoteEdit"></activity>

ここまでで起動

0148 android notepad03.jpg

コンテキストメニューは、コントロールパッド長押し

0149 android notepad04.jpg

(3)

  • ライフサイクルイベントの利用
  • アプリケーション

プロジェクトの作成

  • 同様に、Notepadv3 からプロジェクトを作成。
  • 上記 Notepadv2 を完成させた状態と Notepadv3 は同じ

NoteEdit クラス

onCreate()

  • メンバ変数の宣言
  1. private NotesDbAdapter mDbHelper;
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. mDbHelper = new NotesDbAdapter(this);
  5. mDbHelper.open();
  6. setContentView(R.layout.note_edit);
  7. mTitleText = (EditText) findViewById(R.id.title);
  8. mBodyText = (EditText) findViewById(R.id.body);
  9. Button confirmButton = (Button) findViewById(R.id.confirm);
  10. mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID)
  11. : null;
  12. if (mRowId == null) {
  13. Bundle extras = getIntent().getExtras();
  14. mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
  15. : null;
  16. }
  17. populateFields();
  18. confirmButton.setOnClickListener(new View.OnClickListener() {
  19.  
  20. public void onClick(View view) {
  21. setResult(RESULT_OK);
  22. finish();
  23. }
  24. });
  25. }

populateFields() を作成

  1. private void populateFields() {
  2. if (mRowId != null) {
  3. Cursor note = mDbHelper.fetchNote(mRowId);
  4. startManagingCursor(note);
  5. mTitleText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
  6. mBodyText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
  7. }
  8. }

オーバーライドメソッドを実装

onSaveInstanceState()
  1. @Override
  2. protected void onSaveInstanceState(Bundle outState) {
  3. // TODO Auto-generated method stub
  4. super.onSaveInstanceState(outState);
  5. outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
  6. }
onPause()
  • saveState()は後ほど作成
  1. @Override
  2. protected void onPause() {
  3. // TODO Auto-generated method stub
  4. super.onPause();
  5. saveState();
  6. }
onResume()
  1. @Override
  2. protected void onResume() {
  3. // TODO Auto-generated method stub
  4. super.onResume();
  5. populateFields();
  6. }

saveState() を追加

  1. private void saveState() {
  2. String title = mTitleText.getText().toString();
  3. String body = mBodyText.getText().toString();
  4. if (mRowId == null) {
  5. long id = mDbHelper.createNote(title, body);
  6. if (id > 0) {
  7. mRowId = id;
  8. }
  9. } else {
  10. mDbHelper.updateNote(mRowId, title, body);
  11. }
  12. }

Notepadv3

onActivityResult()

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  3. super.onActivityResult(requestCode, resultCode, intent);
  4. fillData();
  5. }

onListItemClick()

  1. @Override
  2. protected void onListItemClick(ListView l, View v, int position, long id) {
  3. super.onListItemClick(l, v, position, id);
  4.  
  5. Intent i = new Intent(this, NoteEdit.class);
  6. i.putExtra(NotesDbAdapter.KEY_ROWID, id);
  7. startActivityForResult(i, ACTIVITY_EDIT);
  8. }