Android開発 - 画面間のデータ転送

2011/07/12 00:44Update
TAGS: Android | 開発 | 遷移 | データ | パラメータ

画面間の遷移の際、画面間のデータ(もしくはパラメータ)転送の実装方法について。

はじめに


Android開発入門 Hello world
Android開発 - 副画面を作成
Android開発 - 画面間の遷移

画面遷移時、画面間のデータ転送の実装方法


Android開発 - 画面間の遷移をベースに画面間のデータ転送の実装を行いたいと思います。

上の記事では、主画面から副画面遷移の実装方法について学びました。
本文は、遷移を行う際、データの転送について解説します。

■手順
1)主画面にてデータ(パラメータ)を発送
MyAndroidActivity.java
package com.syboos.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyAndroidActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Log.d("MyInfo", "Test output");
        
        //副画面に遷移 ボタン
        Button btnGotoSub = (Button)findViewById(R.id.btnGotoSub);
        //ボタンのクリックイベントリスナー を設定
        btnGotoSub.setOnClickListener(this);
    }

    /* 
     * ボタンクリック処理
     */
    public void onClick(View v) {
        Log.d("MyInfo", v.getId() + v.toString());
        Button b = (Button)v;
        Log.d("MyInfo", "Button");
        
        try {
            if (b.getId() == R.id.btnGotoSub) {
                //id は R.id.btnGotoSub(副画面に遷移ボタン)である
                
                //Intent インスタンス生成
                Intent intent = new Intent(MyAndroidActivity.this, MySubActivity.class);
                
                //データ転送 [name="Test Value"]、[age=20]
                intent.putExtra("name", "Test Value");    //文字列
                intent.putExtra("age", 20);        //int 型数字
                
                //副画面を起動
                startActivity(intent);
                Log.i("MyInfo", "Start activity ok");
            }
        } catch (Exception e) {
            Log.d("MyInfo", "Error", e);
        }
    }
}


※注目
                //データ転送 [name="Test Value"]、[age=20]
                intent.putExtra("name", "Test Value");    //文字列
                intent.putExtra("age", 20);        //int 型数字

Intent.putExtraメソッドについて、次のような型のデータの転送ができます。



2)副画面にデータを受取り

①受け取ったデータを分かるように、副画面に表示項目を追加します。


sub.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="主画面からのデータ:" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content"></TextView>
    <Button android:layout_width="wrap_content" android:id="@+id/btnCloseSub" android:layout_height="wrap_content" android:text="閉じる"></Button>
    
</LinearLayout>


②副画面でデータを受け取る

package com.syboos.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MySubActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i("MyInfo", "MySubActivity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub);
        
        Log.i("MyInfo", "MySubActivity2");
        
        //閉じるボタンを取得
        Button btnCloseSub = (Button)findViewById(R.id.btnCloseSub);
        //閉じるボタンのクリックイベントのリスナーを設定
        btnCloseSub.setOnClickListener(this);
        
        //受け取ったデータを表示するTextViewを取得
        TextView textParam = (TextView)findViewById(R.id.textView1);
        
        //Intentを取得
        Intent intent = getIntent();
        //"name"データを取得
        String name = intent.getStringExtra("name");
        //"age"データを取得
        int age = intent.getIntExtra("age", -1);
        
        //受け取ったデータを表示
        textParam.setText("name:" + name + " age:" + age);
        
        Log.i("MyInfo", "MySubActivity3");
    }

    /* 
     * クリックイベント処理
     */
    public void onClick(View v) {
        Button b = (Button)v;
        if (b.getId() == R.id.btnCloseSub) {
            //閉じるボタンの場合
            
            //画面を閉じる
            this.finish();
        }
    }
}


※ご注目
        //受け取ったデータを表示するTextViewを取得
        TextView textParam = (TextView)findViewById(R.id.textView1);
        
        //Intentを取得
        Intent intent = getIntent();
        //"name"データを取得
        String name = intent.getStringExtra("name");
        //"age"データを取得
        int age = intent.getIntExtra("age", -1);
        
        //受け取ったデータを表示
        textParam.setText("name:" + name + " age:" + age);


3)実行(エミュレータ)&確認
主画面


副画面


ちゃんと表示されましたね。

有关作者
Syboos.jp編集長AJavaやオープンソース情報の執筆、Webサイトの開発や運営全般の業務に携わる。

Sponsored Link


Comments

用户名 (required)

Email (will not be published) (required)

URL

Evaluation