-2

I'm starting Android Studio and I looked at tutorials but none of them seems to work. I have four Strings, spielernr1, spielernr2, spielernr3, and koenig and i got them from EditTexts in the MainActivity class. Now I'd like to use them in my seite1 class, but I don't know how to import them into the seite1 class. Here's the code of the MainActivity class:

public class MainActivity extends AppCompatActivity {
String spielernr1, spielernr2, spielernr3, koenig;
EditText spieler1;
EditText spieler2;
EditText spieler3;
EditText kartenkoenig;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    spieler1 = (EditText) findViewById(R.id.editText);
    spieler2 = (EditText) findViewById(R.id.editText2);
    spieler3 = (EditText) findViewById(R.id.editText3);
    kartenkoenig = (EditText) findViewById(R.id.editText4);
    Button startbutton = (Button) findViewById(R.id.button2);
    startbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            spielernr1 = spieler1.getText().toString();
            spielernr2 = spieler2.getText().toString();
            spielernr3 = spieler3.getText().toString();
            koenig = kartenkoenig.getText().toString();

            setContentView(R.layout.activity_seite1);


        }
    });

}

And here's the code of the seite1:

public class seite1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seite1);



}

}

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
v.k
  • 1
  • 3

4 Answers4

5

If I understood your question, you have an activity that gets some information and wanna use them in another activity. so you can use intents like this code in the first activity when you want to go to second class:

Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
i.putExtra("value", "some string");
startActivity(i);

in second activity use this code:

Bundle extras = getIntent().getExtras();
// get data via the key
String value1 = extras.getString("value");

you can have extras as many as you want

reza
  • 1,746
  • 6
  • 16
  • 32
1

I think you can just put them in Intent:

Pass a String from one Activity to another Activity in Android

or use Bundle: https://developer.android.com/reference/android/os/Bundle.html

Community
  • 1
  • 1
Tim
  • 561
  • 2
  • 13
0

You probably need to read a little bit more on how to start new activities, your doing it wrong.

You should have something like this:

Intent intent= new Intent(MainActivity.this,OtherActivity.class);
starActivity(intent);

This site will help you understand how to start a new activity and send data to it.

Start and Send data

Fabian valencia
  • 132
  • 2
  • 11
  • Your example is not particularly helpful because it doesn't show how to pass the `String`s to the activity being started. Perhaps you could edit it to provide an example? – clownba0t Nov 11 '16 at 16:28
  • Thanks for the comment, did you really check the link I added ? you will find how to add a string to the bundle or using extras. – Fabian valencia Nov 11 '16 at 16:34
  • The link is irrelevant. Aside from the fact it could be dead, the example code you provided simply starts an activity without any extras, whereas the OP will need to know how to add extras (in this case `String` extras) to the intent they use to start the next activity. Please don't misunderstand my intent (no pun intended) - simply trying to make sure the OP has a clear answer :) – clownba0t Nov 11 '16 at 16:39
0

First You Need to Learn About how to use Intents to share data between different activity.

In first Activity-:

Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
i.putExtra("key", "StringToSend");
startActivity(intent);

In Second Activity-:

Intent i=getIntent();
String s = intent.getExtras().getString("key");

Use this s string in anywhere in activity.

Kunal Puri
  • 185
  • 1
  • 9