AndroidDevelopersのチュートリアルのNotepad Exercise 2を実行します。
【手順1】
1.「(AndroidDevelopersチュートリアル)Notepad Tutorialの準備」でダウンロードし、解凍した「Notepadv2」のディレクトリを選択し、右クリックから「コピー」を選択。
1.「Eclipse」のワークスペースのディレクトリに移動し、「Ctrl+V」で貼り付け。
1.「Eclipse 3.7.2 Indigoの起動方法」でEclipseを起動。
2.メニューから「ファイル」⇒「新規」⇒「プロジェクト」を選択。
1.「Android」⇒「Androidプロジェクト」を選択。
2.「次へ」ボタンをクリック。
1.「外部ファイルからプロジェクトを作成」を選択。
2.「Location」の参照ボタンをクリックし、【手順2】でコピーを貼り付けたディレクトリを選択。
3.「次へ」ボタンをクリック。
1.「ターゲット名」は「Android1.5」を選択。
2.「次へ」ボタンをクリック。
1.「完了」ボタンをクリック。
1.以下のようにプロジェクトが読み込まれます。
2.「res/layout/note_edit.xml」が以下様にエラーになる場合は、エラーの「match_parent」を「fill_parent」に書き換えます。
3.「Ctrl+Shift+F」でソースコードをフォーマッティング、「Ctrl+Shift+S」でファイルを保存。
1.以下の様にエラーが出なくなります。
1.「Notepadv2.java」を以下の様に入力。
/*
* Copyright (C) 2008 Google Inc.
*
* 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
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.demo.notepad2;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Notepadv2 extends ListActivity {
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private NotesDbAdapter mDbHelper;
private Cursor mNotesCursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes_list);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
// Get all of the rows from the database and create the item list
mNotesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(mNotesCursor);
// Create an array to specify the fields we want to display in the list
// (only TITLE)
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
// and an array of the fields we want to bind those fields to (in this
// case just text1)
int[] to = new int[] { R.id.text1 };
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.notes_row, mNotesCursor, from, to);
setListAdapter(notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case INSERT_ID:
createNote();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = mNotesCursor;
c.moveToPosition(position);
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
i.putExtra(NotesDbAdapter.KEY_TITLE,
c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
i.putExtra(NotesDbAdapter.KEY_BODY,
c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
startActivityForResult(i, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
switch (requestCode) {
case ACTIVITY_CREATE:
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.createNote(title, body);
fillData();
break;
case ACTIVITY_EDIT:
Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (mRowId != null) {
String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.updateNote(mRowId, editTitle, editBody);
}
fillData();
break;
}
}
}
3. 「Ctrl+Shift+F」でソースコードをフォーマッティング、「Ctrl+Shift+S」でファイルを保存 。
4.「NoteEdit.java」を以下の様に入力。package com.android.demo.notepad2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class NoteEdit extends Activity {
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_edit);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
mRowId = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (title != null) {
mTitleText.setText(title);
}
if (body != null) {
mBodyText.setText(body);
}
}
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText()
.toString());
bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText()
.toString());
if (mRowId != null) {
bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}
});
}
}
5. 「Ctrl+Shift+F」でソースコードをフォーマッティング、「Ctrl+Shift+S」でファイルを保存 。6.「AndroidManifest.xml」を以下の様に入力。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo.notepad2" >
<application android:icon="@drawable/icon" >
<activity
android:name=".Notepadv2"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NoteEdit" />
</application>
</manifest>
7. 「Ctrl+Shift+F」でソースコードをフォーマッティング、「Ctrl+Shift+S」でファイルを保存 。
【手順11】
1.「Androidプロジェクトの実行方法」の手順で、「Androidプロジェクト」を実行。
2.エミュレータが起動しロックを解除し、以下の様にアプリケーションが実行されます。
3.「MENU」ボタンをクリックすると、画面下にメニューが表示されます。
1.「Add Note」ボタンをクリックすると、以下の様に入力画面が表示されます。
2.「Title」と「Body」に適当に文字列を入力します。
3.「Confirm」ボタンをクリック。
1.以下の様に、「test」というノートが作成されます。
2.「test」をクリックします。
1.以下の様に再度ノートの内容が表示されます。
【手順15】
1.ノート名をクリック(長押し)すると、コンテキストメニューが表示されます。
2.「Delete Note」をクリックします。
1.以下の様にノートが削除されました。
以上です。
Notepad+Exercise+2-01.jpg)
Notepad+Exercise+2-02.jpg)
Notepad+Exercise+2-03.jpg)
Notepad+Exercise+2-04.jpg)
Notepad+Exercise+2-05.jpg)
Notepad+Exercise+2-06.jpg)
Notepad+Exercise+2-07.jpg)
Notepad+Exercise+2-08.jpg)
Notepad+Exercise+2-09.jpg)
Notepad+Exercise+2-10.jpg)
Notepad+Exercise+2-11.jpg)
Notepad+Exercise+2-12.jpg)
Notepad+Exercise+2-13.jpg)
Notepad+Exercise+2-14.jpg)
Notepad+Exercise+2-15.jpg)
0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。