Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Anyone managed to complete the extra credit (A Model User) for the Android Model-View-Controller Pattern section?

So far I've setup the two Java files, the homepage where I have the listener for button to submit the details and also linked the elements from my layout to the appropriate variables.

Secondly, I created a second java Class called UserDetail where I setup the custom constructor to hold all of the details. Now I know this is a mess and not close to complete, but can anyone give me a hint of where to go with this. (Second extract, shows this UserDetail model).

Thanks.

EDIT: Just to note, I could get this to work by putting it all in the (main) MyActivity page, but it doesn't seem the right way.

public class MyActivity extends Activity {

    //SETUP VARIABLES FOR DETAILS
    private EditText mUsername;
    private EditText mRealName;
    private EditText mAge;
    //private RadioGroup mSex;
    private EditText mLocation;
    private Button mSubmitButton;

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

        mUsername = (EditText)findViewById(R.id.usernameEditText);
        mRealName = (EditText)findViewById(R.id.realnameEditText);
        mAge = (EditText)findViewById(R.id.ageEditText);
        mLocation = (EditText)findViewById(R.id.locationEditText);
        mSubmitButton = (Button)findViewById(R.id.submitButton);

        //SETUP ON CLICK LISTENER FOR BUTTON
        mSubmitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

Second Java Class

public class UserDetail {
    //Setting all the attributes as variables for user details to be collected
    private String mUsername;
    private String mRealName;
    private int mAge;
    //private RadioGroup mSex;
    private String mLocation;

    //Set custom constructor to hold all variables for each user
    public UserDetail(String username, String realName, int age, String location){
       mUsername = username;
       mRealName = realName;
       mAge = age;
       mLocation = location;
    }

    //Using Code, generate - to autocode getters and setters

    public String getUsername() {
        return mUsername;
    }

    public void setUsername(String username) {
        mUsername = username;
    }

    public String getRealName() {
        return mRealName;
    }

    public void setRealName(String realName) {
        mRealName = realName;
    }

    public int getAge() {
        return mAge;
    }

    public void setAge(int age) {
        mAge = age;
    }

    /*public ToggleButton getSex() {
        return mSex;
    }

    public void setSex(ToggleButton sex) {
        mSex = sex;
    }*/

    public String getLocation() {
        return mLocation;
    }

    public void setLocation(String location) {
        mLocation = location;
    }
}

2 Answers

This probably isn't the answer you want to hear haha. You could send the user data as an intent IF your second class was an activity. However the most logical way would be to add it to an array (either java or xml would work). OR, you could setup an SQLite database to store the data.

Cheers Chris. Setting up two activities seems fairly straightforward, but as you say, does not seem the most logical solution. Will look into adding it to an array. Thanks!