ウェブアプリケーション,インジェクション,コマンドインジェクション

androidアプリケーション開発の基本

アクティビティ間でオブジェクトをやり取りする

Passing Object Between Activities

アクティビティ間のデータのやり取りでは、
アクティビティ間で文字列や数字として単純にデータを
やり取りする方法を示しました。
今回はアクティビティ間のオブジェクトのやり取りを
どのようにするのかを示します。
例えば、アプリケーションのユーザの情報(ユーザID、名前など)を
オブジェクトにまとめてあって、処理したいので他のアクティビティに
そのユーザ情報を渡す必要ができたとします。
この場合、ユーザ情報それぞれを個別に渡す代わりに、
簡単に済みます。
さらに、単純なデータ型をputExtra()とputExtra()メソッドを使い
渡すことは、Intentオブジェクトを使ってオブジェクトを
渡すこともできます。
もし、開発者がカスタムクラスを持っているならば、
そのクラスをSerializableを元にしたクラスで
実行させるようにする必要があります。
以下のコードはMyCustomClass(カスタムクラス)を使った例です。

package net.learn2develop.passingdata;

import java.io.Serializable;
public class MyCustomClassimplements Serializable{
   private static final long serialVersionUID = 1L;
   String _name;
   String _email;

   public void setName(String name){
     _name = name;
   }

   public String Name(){
     return _name;
   }

   public void setEmail(String email){
     _email = email;
   }

   public String Email(){
     return _email;
   }
}

他のアクティビティにオブジェクトを渡すために
putExtra()メソッドを使います。

public void onClick(View view){
   Intent i = new
     Intent("net.learn2develop.SecondActivity");

   //---use putExtra() to add new key/value pairs---
   i.putExtra("str2", "This is a string");
   i.putExtra("age1", 25);

   //---use a Bundle object to add new key/value pairs---
   Bundle extras = new Bundle();
   extras.putString("str2", "This is another string");
   extras.putInt("age2", 35);

   //---attach the Bundle object to the Intent object---
   i.putExtras(extras);

   //---create my own custom object---
   MyCustomClass myObject = new MyCustomClass();
   myObject.setName("<ユーザ名>");
   myObject.setEmail("<ユーザメールアドレス>");
   i.putExtra("MyObject", myObject);

   //---Start the activity to get a result back---
   startActivityForResult(i, 1);
}

他のアクティビティに渡したオブジェクトを検索するためには、
IntentオブジェクトのgetSerializableExtra()メソッドを使い、
getSerializableExtra()メソッドに
最初の方でputExtra()メソッドにセットしたキー(key)を渡します。
putExtra()メソッドで返された結果を
myCustomClassクラスに割り振り、この型の変数にそれを割り当てます。

@Override
public void onCreate(Bundle savesInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_Second);

   //---get the data passed in using getStringExtra()---
   Toast.makeText(this,getIntent().getStringExtra("str1"),
       Toast.LENGTH_SHORT).show();

   //---get the data passed in using getIntExtra()---
   Toast.makeText(this,Integer.toString(
     getIntent().getIntExtra("age1", 0)),
     Toast.LENGTH_SHORT).show();

   //---get the Bundle object passed in---
   Bundle bundle = getIntent().getExtras();

   //get the data using the getString()---
   Toast.makkeText(this, bundle.getString("str2"),
     Toast.LENGTH_SHORT).show();

   //---get the data using the getInt() method---
   Toast.makeText(this,Integer.toString(bundle.getInt("age2")),
     Toast.LENGTH_SHORT).show();

   //---get the custom object passed in---
   MyCustomXlass obj = (MyCustomClass)
     getIntent().getSerializableExtra("MyObject");
   Toast.makeText(this, obj.Name(), Toast.LENGTH_SHORT).show();
   Toast.makeText(this, obj.Email(), Toast.LENGTH_SHORT).show();
}
ホーム
便利堂ロゴ
inserted by FC2 system