0

I am working on an android app where the user fills in one activity, then another one and a word docx report is generated.

I am using extras to pass Strings from the first activity to the second one.

My problem is that all my Strings have the value of the last String put into extras. Here is my code :

Activity 1 :

Intent pelle = new Intent(VGP1.this, Pelle.class);
Bundle extras = new Bundle();
extras.putString(eEntreprise, sEntrepriseComplete);
extras.putString(eAdresse, sAdresseComplete);
extras.putString(eIdVerificateur, editTextIdVerificateur.getText().toString());
extras.putString(eNomProprio, editTextNomProprio.getText().toString());
extras.putString(eAdresseProprio, editTextAdresseProprio.getText().toString());
extras.putString(eNomEntreprise, editTextNomEntreprise.getText().toString());
extras.putString(eAdresseEntreprise, editTextAdresseEntreprise.getText().toString());
extras.putString(eJourVerif, sJourVerif);
extras.putString(eMoisVerif, sMoisVerif);
extras.putString(eAnneeVerif, sAnneeVerif);
pelle.putExtras(extras);
startActivity(pelle);

Activity 2 :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pelle);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        eEntreprise2 = extras.getString(VGP1.eEntreprise);
        eAdresse2 = extras.getString(VGP1.eAdresse);
        eIdVerificateur2 = extras.getString(VGP1.eIdVerificateur);
        eNomProprio2 = extras.getString(VGP1.eNomProprio);
        eAdresseProprio2 = extras.getString(VGP1.eAdresseProprio);
        eNomEntreprise2 = extras.getString(VGP1.eNomEntreprise);
        eAdresseEntreprise2 = extras.getString(VGP1.eAdresseEntreprise);
        eJourVerif2 = extras.getString(VGP1.eJourVerif);
        eMoisVerif2 = extras.getString(VGP1.eMoisVerif);
        eAnneeVerif2 = extras.getString(VGP1.eAnneeVerif);
    }

All the values "xxxxx2" take the value of eAnneeVerif, I don't understand why.

Thank you in advance.

Stone Edge
  • 52
  • 11
  • can u show VGP1 file here ? – Shivang Trivedi Sep 09 '15 at 09:47
  • what are VGP1.eJourVerif, VGP1.eMoisVerif and VGP1.eAnneeVerif ? – pskink Sep 09 '15 at 09:47
  • `public class VGP1 extends Activity { public static final String eEntreprise = null; public static final String eAdresse = null; public static final String eIdVerificateur = null; public static final String eNomProprio = null; public static final String eAdresseProprio = null; public static final String eNomEntreprise = null; public static final String eAdresseEntreprise = null; public static final String eJourVerif = null; public static final String eMoisVerif = null; public static final String eAnneeVerif = null;` – Stone Edge Sep 09 '15 at 09:48
  • all your keys are `null`, that's why you have the same values – pskink Sep 09 '15 at 09:52
  • how to I declare the Strings then? (and just for interest : why does it take one value instead of not taking any?) – Stone Edge Sep 09 '15 at 09:53
  • just use different keys: the first param to Bundle.putString/getString is a "key" – pskink Sep 09 '15 at 09:54

1 Answers1

1

Try this code:

In your Activity 1 send data like this:

Intent pelle = new Intent(VGP1.this, Pelle.class);
pelle.putExtra("eEntreprise", sEntrepriseComplete);
pelle.putExtra("eAdresse", sAdresseComplete);
pelle.putExtra("eIdVerificateur", editTextIdVerificateur.getText().toString());
pelle.putExtra("eNomProprio", editTextNomProprio.getText().toString());
pelle.putExtra("eAdresseProprio", editTextAdresseProprio.getText().toString());
pelle.putExtra("eNomEntreprise", editTextNomEntreprise.getText().toString());
pelle.putExtra("eAdresseEntreprise", editTextAdresseEntreprise.getText().toString());
pelle.putExtra("eJourVerif", sJourVerif);
pelle.putExtra("eMoisVerif", sMoisVerif);
pelle.putExtra("eAnneeVerif", sAnneeVerif);
startActivity(pelle);

In your Activity 2 get data like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pelle);

    Intent intent = getIntent();
    eEntreprise2 = intent.getStringExtra("eEntreprise");
    eAdresse2 = intent.getStringExtra("eAdresse");
    eIdVerificateur2 = intent.getStringExtra("eIdVerificateur");
    eNomProprio2 = intent.getStringExtra("eNomProprio");
    eAdresseProprio2 = intent.getStringExtra("eAdresseProprio");
    eNomEntreprise2 = intent.getStringExtra("eNomEntreprise");
    eAdresseEntreprise2 = intent.getStringExtra("eAdresseEntreprise");
    eJourVerif2 = intent.getStringExtra("eJourVerif");
    eMoisVerif2 = intent.getStringExtra("eMoisVerif");
    eAnneeVerif2 = intent.getStringExtra("eAnneeVerif");

}
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • That's what I have been starting with at first, but as @pskink noticed, the problem is in the declaration of the variables in the Activity 1. I'll still go back to this code (the one you posted) because it's a bit cleaner. – Stone Edge Sep 09 '15 at 09:56
  • dont use "string" literals directly: use String constants instead – pskink Sep 09 '15 at 09:59
  • removing declarations and using String constants made the trick, thank you! – Stone Edge Sep 09 '15 at 11:39