0

I have a little problem with my code. I want to transfer three int values in to my next activity. Unfortunatelly my following code returns me 3x one value. For example predkosc = 5 kat = 10 ramie = 15 and intent returns my in second activity predkosc = 15 kat = 15 ramie = 15 What is wrong iny my code? Code A:

public  static String predkosc_VALUE = "";
public  static String kat_VALUE = "";
public  static String ramie_VALUE = "";
    Intent intent = new Intent(this, Rzut_katapulta.class);
    EditText Text_predkosc = (EditText) findViewById(R.id.editText1);
    EditText Text_kat = (EditText) findViewById(R.id.editText2);
    EditText Text_ramie = (EditText) findViewById(R.id.editText3);
    String message_predkosc = Text_predkosc.getText().toString();
    int predkosc = Integer.parseInt(message_predkosc);
    String message_kat = Text_kat.getText().toString();
    int kat = Integer.parseInt(message_kat);
    String message_ramie = Text_ramie.getText().toString();
    int ramie = Integer.parseInt(message_ramie);

    intent.putExtra(predkosc_VALUE, predkosc);
    intent.putExtra(kat_VALUE, kat);
    intent.putExtra(ramie_VALUE, ramie);

    startActivity(intent);

Code B:

Intent intent = getIntent();
predkosc = intent.getIntExtra(Main_menu.predkosc_VALUE,0);
katy = intent.getIntExtra(Main_menu.kat_VALUE,0);
ramie = intent.getIntExtra(Main_menu.ramie_VALUE,0);
Blackchart
  • 135
  • 1
  • 3
  • 13

3 Answers3

1
public  static String predkosc_VALUE = "";
public  static String kat_VALUE = "";
public  static String ramie_VALUE = "";

Those keys are used to map the values in the intent, since all 3 are the same (empty) you are overwriting the value :)

change them to

public  static String predkosc_VALUE = "first";
public  static String kat_VALUE = "second";
public  static String ramie_VALUE = "third";

and it will work

Daniel Bo
  • 2,518
  • 1
  • 18
  • 29
0

You may need to use the Bundle to send data from one activity to another. Pls Refer this: Passing data from one activity to another using bundle - not displaying in second activity

Community
  • 1
  • 1
Aneesh
  • 279
  • 3
  • 11
0
public  static String predkosc_VALUE = "";
public  static String kat_VALUE = "";
public  static String ramie_VALUE = "";

you have not initialised these variables! try:

 public  static String predkosc_VALUE = "myString1";
    public  static String kat_VALUE = "myString2";
    public  static String ramie_VALUE = "myString3";
EsmaeelQash
  • 488
  • 2
  • 6
  • 20