に複数の追加を追加したいIntent
。1つはaを保持しdouble
、もう1つは保持しlong
ます。これは可能ですか?
もしそうなら、私はそれをどのように行うのでしょうか、そしてどのように私はそれぞれのエキストラから情報を得るのですか?
に複数の追加を追加したいIntent
。1つはaを保持しdouble
、もう1つは保持しlong
ます。これは可能ですか?
もしそうなら、私はそれをどのように行うのでしょうか、そしてどのように私はそれぞれのエキストラから情報を得るのですか?
あなたの心が望むだけ多くのエクストラを追加することができIntent
ます、それらはすべて単なるキーバリューデータです:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
また、同じキー名を使用して取得できます。
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(@ExtraDoubleKey, @ExtraDoubleValue);
intent.putExtra(@ExtraLongKey, @ExtraLongValue);
ここで、@ ExtraDoubleKeyは、エクストラ(つまり、「価格」など)にアクセスするために使用する文字列であり、@ ExtraDoubleValueは、エクストラの値(渡したいdouble変数)です。@ExtraLongKeyと@ExtraLongValueについても同様です。
次に、次のアクティビティでエクストラにアクセスするには、次を使用できます。
double doubleValue = getIntent().getExtras().getDouble(@ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(@ExtraLongKey);
キー@ExtraDoubleKeyを使用してdoubleextraの値を取得します。
https://stackoverflow.com/a/11461530/776075
Devunwired は正しいです。しかし、私が見る方法は、タイプごとに 1 つの値しか保持できないということです。1 つの文字列、1 つの int、1 つの double などのように..
2 つの文字列値を含めることはできません。または 2 つの整数。私はプログラムでこれを経験しており、1 つの文字列と 1 つのブール値を使用してそれを克服しました。
Bundle
これを使用して、パラメーターとしてインテントに渡すことができます。
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent