Reputation: 53
I'm trying to send data to mysql database using jdbc connector. I used EditText for the user to fill their info. I'm trying to get string values from EditText but they return null . This is my acvitivity class and I can't figure out why the string values return empty string.
String setFirstname = "";
String setLastname = "";
String setCountry = "";
String setCity = "";
String setCompany = "";
String setAddress = "";
String setEmail = "";
String setUsername = "";
String setPhone = "";
String setPassword = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
final EditText getFirstname = (EditText) findViewById(R.id.reg_firstname);
setFirstname = getFirstname.getText().toString().trim();
final EditText getCountry = (EditText) findViewById(R.id.reg_country);
setCountry= getCountry.getText().toString().trim();
final EditText getLastname = (EditText) findViewById(R.id.reg_lastname);
setLastname= getLastname.getText().toString().trim();
final EditText getCity = (EditText) findViewById(R.id.reg_city);
setCity= getCity.getText().toString().trim();
final EditText getCompany = (EditText) findViewById(R.id.reg_company);
setCompany= getCompany.getText().toString().trim();
final EditText getAddress = (EditText) findViewById(R.id.reg_address);
setAddress= getAddress.getText().toString().trim();
Button reg = (Button) findViewById(R.id.btnRegister);
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new readySQL().execute();
}
});
// Listening to Login Screen link
loginScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Closing registration screen
// Switching to Login Screen/closing register screen
finish();
}
});
Upvotes: 0
Views: 1021
Reputation: 749
You shouldn't get your editText value when you initialize it, instead you have to set a listener on your editText to detect when the value is changed, something like this:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(final CharSequence s, final int start,
final int before, final int count) {
//collect the changed value
setValue=(editText.getText().toString().trim());
}
@Override
public void beforeTextChanged(final CharSequence s,
}
@Override
public void afterTextChanged(final Editable s) {
}
});
Upvotes: 0
Reputation: 1289
You are getting the string when the edit texts are created .The obviously are empty at that time. The user has not entered anything yet. Instead you should be doing this
Button reg = (Button) findViewById(R.id.btnRegister);
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Fetch strings from edit texts here and check if they are not empty
new readySQL().execute();
}
});
Upvotes: 4