2つの値を別のアクティビティに渡したいのですが、putExtraでこれを行うことができますか、それとももっと複雑な方法で行う必要がありますか。たとえば、このようなものは機能しますか?
public final static String ID_EXTRA="com.fnesse.beachguide._ID";
Intent i = new Intent(this, CoastList.class);
i.putExtra(ID_EXTRA, "1", "111");
startActivity(i);
上記はエラーになります。
編集
私が最初に試したのは次のようなものでした:
i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");
しかし、ID_EXTRA2はID_EXTRA1を上書きしているようです
そう、
i.putExtra(ID_EXTRA, new String[] { "1", "111"});
外見的には見えますが、2番目のアクティビティで配列から値を抽出するにはどうすればよいですか。これを単一の値に使用しています。
passedVar = getIntent().getStringExtra(CoastList.ID_EXTRA);
どういうわけかID_EXTRAを配列に変換する必要があると思いますか?
複数のキーを使用して、複数の値を渡すことができます。の代わりに
i.putExtra(ID_EXTRA, "1", "111");
行う
i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");
もちろん、キーに2つの定数を定義し、新しいアクティビティで両方を別々に読み取る必要があります。
または、文字列配列を渡すことができます
i.putExtra(ID_EXTRA, new String[] { "1", "111"});
必要に応じて、個々のエキストラではなく、エキストラの「バンドル」を渡すことができます。たとえば、
Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_USERNAME","my_username");
extras.putString("EXTRA_PASSWORD","my_password");
intent.putExtras(extras);
startActivity(intent);
次に、トリガーするアクティビティで、次のようにこれらを参照できます。
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("EXTRA_USERNAME");
String password_string = extras.getString("EXTRA_PASSWORD");
または(必要に応じて):-
Bundle extras = getIntent().getExtras();
String username_string = extras.getString("EXTRA_USERNAME");
String password_string = extras.getString("EXTRA_PASSWORD");
お役に立てれば! :-)
クラスに追加の値を入れる
public class MainActivity extends Activity {
public final static String USERNAME = "com.example.myfirstapp.MESSAGE";
public final static String EMAIL = "com.example.myfirstapp.EMAIL";
public void registerUser(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText userNameTxt = (EditText) findViewById(R.id.editText1);
EditText emailTxt = (EditText) findViewById(R.id.editText2);
String userName = userNameTxt.getText().toString();
String email = emailTxt.getText().toString();
intent.putExtra(USERNAME, userName);
intent.putExtra(EMAIL,email);
startActivity(intent);
}
別のクラスから追加の値を読み取る
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String user = intent.getStringExtra(MainActivity.USERNAME);
String email = intent.getStringExtra(MainActivity.EMAIL);
いいえ、できませんが、次を使用して配列を渡すことができます。
public Intent putExtra (String name, String[] value)
たとえばこのように:
i.putExtra(ID_EXTRA, new String[]{"1", "111"});
Extras
はKey
とValue
から作成されるため、この例は機能しません。複数のKeys
とValues
を1つのExtra
に入れることはできません
また、私が知っている(そして気づいた)限り、あなたのキーはStrings
である必要がありますが、私はそのキーが間違っているかもしれません。
これを試して:
public final static String ID_EXTRA="com.fnesse.beachguide._ID";
Intent i = new Intent(this, CoastList.class);
i.putExtra("ID_Extra", ID_EXTRA);
i.putExtra("1", 111);
startActivity(i);
単一の小さな値putExtraは完璧ですが、複数の値を渡し、カスタムArrayListを渡す必要がある場合、またはListその後、私はそれは悪い考えだと思います。解決策は、Parcelableを使用できることです。また、SingletonまたはSerializableも。
小包:
共有する必要があるパーセル可能なクラス。
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
送信したいアクティビティを作成します
MyParcelable example = new MyParcelable();
.....
.....
Bundle bundle = new Bundle();
bundle.putParcelable("example", Parcels.wrap(example));
受け取りたい別のアクティビティから
MyParcelable example = Parcels.unwrap(getIntent().getParcelableExtra("example"));