Notepad Tutorial (2)

へろへろな上に明日もお仕事なのでざくっとヤッツケて早めに寝る方向。

とりあえず

Step 2 はざっくり中身を見るだけにしといて以降をナニ。
Step 3 では res/layout/notepad_list.xml を開け、とある。開いてみたら以下なカンジ。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</LinearLayout>

そーか、java なんだよね。XML 嫌いなんですが、iPhoneAndroidXML が色んなトコで使われるのか。そりゃ良いとして、このケイスでは LinearLayout というクラスを使っているのが分かる。Layout タブに切り替えてみると、結構色んな Layout があるのが分かる。
で、Step 4 で以下のようにせい、とある。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

  <ListView android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_notes"/>

</LinearLayout>

盛り込んでみる。なんとなく、リストに出力すべきものが無い場合は TextView が出力、みたいな事が書いてある模様。

Step 5 でもう一つファイルを作れ、とある。res/layout/notes_row.xml との事。res の中の layout を選択して右クリックから New -> other から Android の中にある Android XML File を選択して next ってメモ取りながら進めてたら、wizard にて next できなくなったので eclipse 再起動。(<- 今ココ
で、再起動後も作成できぬ。仕方がないので notepad_list.xml をコピーして無理矢理作成。以下をコピペ。

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

どうもこれ、上記リストの一行分のナニな模様。@+id とかな形になってると android が id を自動採番してくれるのかな。
次は Step 6 ですが、いよいよ Notepadv1 クラスをナニ。三つのメソドを override との事。

  • onCreate Activity が start した時に呼ばれるメソド。
  • onCreateOptionsMenu メニューを Activity に配置するのか。メニューなボタンのクリック時とかに表示される、との事。
  • onOptionsItemSelected 名前の通り、item 選択時に、な callback なのかな

onCreate

以下に、との事。

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notepad_list);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        fillData();
    }

してるコトは順に以下。

  • super クラスの onCreate メソド呼び出し
  • notepad_list なレイアウト適用
  • NotesDbAdapter なオブジェクト作って open メソド呼び出し
  • fillData メソド呼び出し

mDbHelper な属性は private で定義せよ、との事。もう少し頑張ってみる。

onCreateOptionsMenu

string なリソース追加との事。追加後のナニが以下。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Notepad v1</string>
    <string name="no_notes">No Notes Yet</string>
    <string name="menu_insert">Add Item</string>
</resources>

で、クラス定義の先頭に以下を追加せよ、とある。

public static final int INSERT_ID = Menu.FIRST;

で、メソド定義を以下に。

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return result;
    }

_Add Item_なメニュー項目が追加される、と。ちなみに add メソドに渡すソレについての解説あり。

The arguments passed to add() indicate: a group identifier for this menu (none, in this case), a unique ID (defined above), the order of the item (zero indicates no preference), and the resource of the string to use for the item.

もうちょいですが、へろへろなのでこれで止めます。明日は現実トウヒの時間があると信じたいですが、どうなる事やら。