c. android SDK 2

参照: @ITの記事

・Fragment による画面の分割とコンテンツ表示の切り替え: Androidの画面の大きさの違いを解決するFragments

・SurfaceViewによるゲーム画面描画: SurfaceViewならAndroidで高速描画ゲームが作れる

・SQLiteによるデータ永続化: AndroidでSQLiteのDB操作をするための基礎知識

参照: CodeZine の記事

・HTTPによる通信: Web APIで楽々Androidアプリ

演習内容:

Web上の記事を参照して、android アプリの機能について学び、サンプルアプリを作成、動作を確認する。

■準備:

各自、指定されたPCに着席(先週と同じPC)。

AVTは、HAXMによる高速化エミュレータを使用するので管理者権限が必要。

自分のIDでログインするのではなく、SAが準備したID advprog2 でログインした状態で演習に取り組む。

課題の提出は、各PC内の eclipse のワークスペースを、レポートフォルダの学籍番号付きフォルダにコピーして提出する。

■演習1:

Fragment の利用。

プロジェクトおよびアプリケーション名 Sample2013_12b

最低限のAPI API 18: android 4.3

theme None

Create Activity

Master / detail Flow

スケルトンプログラムの実行確認。

左にリストメニュー、右にコンテンツが表示されるアプリケーションである。

メニューとコンテンツの表示に Fragments を利用している。

解説:

アプリケーション設定ファイル:

AndroidManifest.xml

        <activity
            android:name="com.example.sample2013_12b.ItemListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
ItemListActivity がメインのActivity。
        <activity
            android:name="com.example.sample2013_12b.ItemDetailActivity"
            android:label="@string/title_item_detail"
            android:parentActivityName=".ItemListActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ItemListActivity" />
        </activity>
別のActivity ItemDetailActivity も存在する。親は、ItemListActivity

これは、2ペイン表示が出来ない場合の、表示切り換え用 Activity

2ペイン表示のレイアウト情報は以下を参照

activity_item_twopane.xml

See res/values-large/refs.xml and
    res/values-sw600dp/refs.xml for an example of layout aliases
    that replace the single-pane version of the layout with
    this two-pane version.

左右のフラグメントの幅の比率が1:3となるように設定されている。

■練習

比率が 1:1 となる様に設定を変更、左右の各Fragment の weight を 同じ値にする。

android:layout_weight="1"

ItemListActivity.java

protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.activity_item_list);

R.layout.activity_item_list

2ペイン表示可能かどうかで(タブレットかスマフォかで)画面の表示方法を変更。

public void onItemSelected(String id) {

if (mTwoPane) {

コールバックの設定:

public class ItemListActivity extends FragmentActivity implements

ItemListFragment.Callbacks

メニュー選択時に

public void onItemSelected(String id) {

が呼び出される。

Fragment の画面の登録と表示の管理は、 FragmentMaganer を通して行う。

メニュー番号の設定

              arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
コンテンツペイン(Fragment)の生成

ItemDetailFragment fragment = new ItemDetailFragment();

メニュー番号を、コンテンツペインに渡す

fragment.setArguments(arguments);

フラグメントの切り替え

getSupportFragmentManager().beginTransaction()

.replace(R.id.item_detail_container, fragment).commit();

ItemListFragment.java

public void onListItemClick(ListView listView, View view, int position,

long id) {

super.onListItemClick(listView, view, position, id);

// Notify the active callbacks interface (the activity, if the

// fragment is attached to one) that an item has been selected.

mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);

}

リストメニューがクリックされた際には、 DummyContent クラスの クリックされたメニューの位置に対応する id がセットされて、メインのアクティビティに制御が返り、idに対応したコンテンツ詳細が表示される。

メニューの表示項目は、以下のパッケージ内で定義されている。

DummyContent.java

package com.example.sample2013_12b.dummy;

■練習

リストメニューの表示項目の設定部分に、変更を加える。1,2,3 を a,b,c に。 4番目のメニューを追加。

public class DummyContent {

/**

* An array of sample (dummy) items.

*/

public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();

/**

* A map of sample (dummy) items, by ID.

*/

public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();

static {

// Add 3 sample items.

addItem(new DummyItem("1", "Item a"));

addItem(new DummyItem("2", "Item b"));

addItem(new DummyItem("3", "Item c"));

addItem(new DummyItem("4", "Item d"));

}

コンテンツ詳細のコード:

ItemDetailFragment.java

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (getArguments().containsKey(ARG_ITEM_ID)) {

// Load the dummy content specified by the fragment

// arguments. In a real-world scenario, use a Loader

// to load content from a content provider.

mItem = DummyContent.ITEM_MAP.get(getArguments().getString(

ARG_ITEM_ID));

}

詳細ペインの生成時に、メニュー番号に応じたコンテンツを DummyContent から取得。

詳細ペインの表示は、

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_item_detail,

container, false);

で、 R.layout.fragment_item_detail に設定された、レイアウトを使用する。レイアウトには、 TextView が設定されている。

以下で、fragment_item_detail.xml の item_detail という id のコンポーネント(TextView)に、 DummyContent で定義された対応idの content をテキストとして設定している。

((TextView) rootView.findViewById(R.id.item_detail))

.setText(mItem.content);

■練習

詳細コンテンツを テキストで登録可能なように、コードを修正する。

DummyItem クラスの修正:

static {

addItem(new DummyItem("1", "Item a", "aaa"));

addItem(new DummyItem("2", "Item b", "bbb"));

addItem(new DummyItem("3", "Item c", "ccc"));

addItem(new DummyItem("4", "Item d", "ddd"));

}

public static class DummyItem {

public String id;

public String content;

public String detail;

public DummyItem(String id, String content,String detail) {

this.id = id;

this.content = content;

this.detail = detail;

}

コンテンツ詳細を 詳細ペインに設定するようコードを修正:

ItemDetailFragment クラスの onCreateView() で、

((TextView) rootView.findViewById(R.id.item_detail))

.setText(mItem.detail);

■演習2

プロジェクトおよびアプリケーション名 Sample2013_12c

ページの冒頭で紹介したWebの記事を参考に、アプリケーションを作成する。

・Fragmentを利用するアプリを1からコードで作成する記事

・ゲーム用の高速書き換え可能なグラフィック画面を利用するアプリの記事

・android 内蔵データベース SQLite を利用して、データ保存を行うアプリの記事

・ネットに公開されている情報サービスを、WebAPIを利用してアプリで表示する記事

のいずれかから選ぶ、若しくは、自分で作成してみたいアプリの機能を検索し、入門や作成記事探し出して各自で取り組む。