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

Raymond Mwaura
Raymond Mwaura
49 Points

Pass widget input to DatabaseHelperSource file

Disclaimer: I am a newbie to Android development :)

How can I pass the string values collected from this first class to the class below? I attempted this but only got null values.

Here's my main activity.

public class Register extends AppCompatActivity {

protected SnapToSellDataSource mDataSource;

public String sFullname;
public String sEmail;
public String sMobileNumber;
public String sPassword;

EditText full_name, email, mobile_number, pwd, copwd;
Button registerButton;

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

    mDataSource = new SnapToSellDataSource(Register.this);

    full_name = (EditText) findViewById(R.id.editText);
    email = (EditText) findViewById(R.id.editText2);
    mobile_number = (EditText) findViewById(R.id.editText3);
    pwd = (EditText) findViewById(R.id.editText4);
    copwd = (EditText) findViewById(R.id.editText5);
    registerButton = (Button) findViewById(R.id.button);

    registerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Register register = new Register();

            String editPassword = pwd.getText().toString();
            String editConfirmPassword = copwd.getText().toString();

                if(editPassword.equals(editConfirmPassword)) {

                //This isn't overwriting the null class variables I 
                //instantiated so that I can pass them to the class below

                sFullname = full_name.getText().toString();
                sEmail = email.getText().toString();
                sMobileNumber = mobile_number.getText().toString();
                sPassword = pwd.getText().toString();

                mDataSource.insertUser(register);
                }
        }
    });
}
}

Here's the class that should receive the string values:

public class SnapToSellDataSource {

private SQLiteDatabase mDatabase;
private  SnapToSellHelper mHelper;
private Context mContext;

public SnapToSellDataSource(Context context){
    mContext = context;
    mHelper = new SnapToSellHelper(mContext);
}

public void insertUser(Register register){
    ContentValues values = new ContentValues();
    values.put(SnapToSellHelper.COL_NAME, register.sFullname);
    values.put(SnapToSellHelper.COL_EMAIL, register.sEmail);
    values.put(SnapToSellHelper.COL_NUMBER, register.sMobileNumber);
    values.put(SnapToSellHelper.COL_PASSWORD, register.sPassword);

    mDatabase.insert(SnapToSellHelper.TBL_USERS, null, values);
}
}