0

In my app, I need to capture multiple data from user when they signup. To avoid multiple EditText from being packed into one layout I have decided to use multiple layouts from within one activity. One layout will capture emails, another phone numbers and so on. When I enter data into an EditText in one layout, a button takes me to the next. What I have done (As a test) is this:

SignupActivity

public class SignupActivity extends AppCompatActivity {

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

    final List<String> myData = new ArrayList<>();
    final EditText displayNameEditText = (EditText) findViewById(R.id.displayNameEditText);

    Button mainNextButton = (Button) findViewById(R.id.nextButtonMain);
    mainNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myData.add(displayNameEditText.getText().toString());
            setContentView(R.layout.phone_numbers_capture);
            final EditText phone1EditText = (EditText) findViewById(R.id.phone1EditText);
            Button phoneNextButton = (Button) findViewById(R.id.phoneNumbersUINextButton);
            phoneNextButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    myData.add(phone1EditText.getText().toString());
                    Log.e("TAG IT", "Full name : " + myData.get(0));
                    Log.e("TAG IT", "Phone 1 : " + myData.get(1));
                }
            });

        }
    });
}
}

The output I get is this (LogCat)

enter image description here

I can then pass this info from myData like this when I get to the last layout:

MyObject myObject = new MyObject();
myObject.setFullName(myData.get(0)) //Method expects a String, no issues
myObject.setPhoneNumberOne(myData.get(1)) //Method expects a string, no issues

Now I feel this isn't the best way to do this. First of all, What If I want to capture different data types from each layout?

List<Object> myData comes to mind, and then what? What other approach can I use to achieve this? If I leave it as is, what problems can I encounter in the future, besides the obvious datatype one I mentioned?

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132

3 Answers3

0

if you want to split your registration page into multiple subpage pages then i would recommend fragments.

For example if you have 3 sections in a single registration then add three fragments may be in a viewpager for better UX

all the data can be directly access through Activity using interface b/w fragments and activity

Jayakrishnan
  • 4,457
  • 29
  • 29
  • Okay, So I found a way to do this [here](http://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts) but then, how do I get the data from each fragment into the activity hosting them? – Ojonugwa Jude Ochalifu Nov 27 '16 at 13:31
  • Using getter and setter method or better use interface class implemented on Activty – Jayakrishnan Nov 27 '16 at 13:42
0

You can get all data from 1st activity put it into the bundle then put bundle in intent's extra and in 2nd activity get bundle add new data and do everything again.

Bundle bundle = new Bundle();
bundle.putString("Name", "full name");
...
intent.putExtras(bundle);

In 2nd activities use intent.getExtras() to obtain data

Steve
  • 81
  • 2
  • 10
0

What's the reason behind storing data in the list? Just write directly to your data object in this case MyObject, but consider naming it more precisely.

If you need to store multiple phone number or email values. Consider adding lists to your MyObject` data structure.

Andrej Jurkin
  • 2,216
  • 10
  • 19