Search code examples
androidlayout-inflater

Android, LayoutInflater not working


I need your help with this Inflater. From Activity3 I need to get what inside an EditText in Activity1.

Ex: in Activity1 i write my name, email, ecc. In Activity2 I choose my birthday and in the Activity3 i want to see both.

So i used in the Activity3 the LayoutInflater, but when i parse the EditText to string, the string is empty.

Here's the code:

    LayoutInflater inflater = getLayoutInflater();
    View v1 = inflater.inflate(R.layout.activity_register1, null);
    etName = (EditText) v1.findViewById(R.id.etName);
    etLastName = (EditText) v1.findViewById(R.id.etLastName);
    etEmail = (EditText) v1.findViewById(R.id.etEmail);
    etPassword = (EditText) v1.findViewById(R.id.etPassword);
    actvCity = (AutoCompleteTextView) v1.findViewById(R.id.actvCity);
    etDescription = (EditText) v1.findViewById(R.id.etDescription);

    View v2 = inflater.inflate(R.layout.activity_register2, null);
    datePicker = (DatePicker) v2.findViewById(R.id.datePicker);
    birthday = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());

    btnRegister = (Button) findViewById(R.id.btnRegister);

    /** Register Button Click event */
    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = etName.getText().toString().trim();
            String lastName = etLastName.getText().toString().trim();
            String email = etEmail.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            String description = etDescription.getText().toString().trim();
            String city = actvCity.getText().toString().trim();
            String dateString = DateFormat.getDateInstance().format(birthday.getTime());

            //if (!name.isEmpty() && !lastName.isEmpty() && !email.isEmpty() && !password.isEmpty() && !city.isEmpty()) 
                Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
            //}

        }
    });

}

The dateString works. But with all the other strings are empty ("").

How can i fix this or tell me if there is another solution to this porblem.

Thanks.


Solution

  • The layout you are inflating in Activity3 using inflater.inflate() is different from what you might have inflated in Activity1 or Activity2.

    You cannot use the data entered in different activities just by inflating the layout. You need to use getIntent() in the receiving activity and set that required data in the intent (used in startActivity(intent)) on the sender activity's side.

    So you need to pass your values from Activity1 to Activity2 first , then fetch the data in Activity2 and then again send the combined data of Activity1 and Activity2 to Activity3. For more details on data transfer using intents check out this.