getExtra()
誰か、インテントに対してとを正確にどのように使用するか教えてくださいputExtra()
。実際、文字列データを格納する str という文字列変数があります。ここで、このデータをあるアクティビティから別のアクティビティに送信したいと考えています。
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra(strName, keyIdentifer );
そしてSecondScreen.javaで
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
TextView userName = (TextView)findViewById(R.id.userName);
Bundle bundle = getIntent().getExtras();
if(bundle.getString("strName")!= null)
{
//TODO here get the string stored in the string variable and do
// setText() on userName
}
}
非常に基本的な質問だとはわかっていますが、残念ながらここで行き詰まってしまいます。助けてください。
ありがとう、
編集: ここで、ある画面から別の画面に渡そうとしている文字列は動的です。つまり、ユーザーが入力した文字列を取得する editText があります。次に、 の助けを借りて、myEditText.getText().toString()
入力された値を文字列として取得し、このデータを渡す必要があります。
ベストアンサー1
これを使用してファイルを「配置」します...
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String strName = null;
i.putExtra("STRING_I_NEED", strName);
次に、値を取得するには、次のようにします。
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}