Ajuda com atualizar cadastro, consulta e gravação no DB

  • Respostas:0
  • AbertoNão stickySem resposta
  • Posts no fórum: 1

20/04/2018, 04:10:42 via Web

Gostaria de uma ajuda, quando eu entro em atualizar dados o campo somente a data de nascimento não me retorna nenhum valor e quando eu coloco qualquer data e mando atualizar ele duplica o usuário, tudo isso começou quando eu inseri o código a baixo em vermelho.
segue o código abaixo:

ChangeRegisterActivity.java

*public class ChangeRegisterActivity extends BaseActivity {
private static final String TAG = ChangeRegisterActivity.class.getSimpleName();
ImageView pictureImageView;
EditText nameEditText;
CpfEditText cpfEditText;
EmailEditText emailEditText;
EditText dddEditText;
EditText phoneNumberEditText;
DateEditText birthdayEditText;
EditText addressEditText;
EditText numberEditText;
EditText complementEditText;
EditText neighborhoodEditText;
Spinner stateSpinner;
EditText cityEditText;
Button saveButton;
private TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
validateInput();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_register);
Toolbar toolbarView = findViewById(R.id.toolbar);
if (null != toolbarView) {
setSupportActionBar(toolbarView);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
User user = Preferences.getUser(this);
if(user != null) {
pictureImageView = findViewById(R.id.picture_img);
nameEditText = findViewById(R.id.complete_name);
cpfEditText = findViewById(R.id.cpf);
emailEditText = findViewById(R.id.email);
dddEditText = findViewById(R.id.ddd);
phoneNumberEditText = findViewById(R.id.phone_number);
birthdayEditText = findViewById(R.id.birthday);
addressEditText = findViewById(R.id.address);
numberEditText = findViewById(R.id.address_number);
complementEditText = findViewById(R.id.complement);
neighborhoodEditText =findViewById(R.id.neighborhood);
stateSpinner = findViewById(R.id.state);
cityEditText =findViewById(R.id.city);
saveButton = findViewById(R.id.save_button);
nameEditText.addTextChangedListener(mTextWatcher);
cpfEditText.addTextChangedListener(mTextWatcher);
emailEditText.addTextChangedListener(mTextWatcher);
dddEditText.addTextChangedListener(mTextWatcher);
phoneNumberEditText.addTextChangedListener(mTextWatcher);
birthdayEditText.addTextChangedListener(mTextWatcher);
addressEditText.addTextChangedListener(mTextWatcher);
numberEditText.addTextChangedListener(mTextWatcher);
neighborhoodEditText.addTextChangedListener(mTextWatcher);
cityEditText.addTextChangedListener(mTextWatcher);
nameEditText.setText(user.getName());
cpfEditText.setText(user.getCpf());
emailEditText.setText(user.getEmail());
dddEditText.setText(user.getPhoneCode());
phoneNumberEditText.setText(user.getPhone());
birthdayEditText.SetText(user.getBirthday());
addressEditText.setText(user.getAddressStreet());
numberEditText.setText(user.getAddressNumber());
complementEditText.setText(user.getAddressComplement());
neighborhoodEditText.setText(user.getAddressNeighborhood());
selectSpinnerValue(stateSpinner, user.getAddressState());
cityEditText.setText(user.getAddressCity());
}
}
@Override
protected void onResume() {
super.onResume();
Bitmap pictureBitmap = ImageUtil.getBitmapFromFile(getExternalFilesDir(null).getAbsolutePath() + "/pic.jpg");
Resources res = getResources();
RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(res, pictureBitmap);
dr.setCircular(true);
pictureImageView.setImageDrawable(dr);
}
private void selectSpinnerValue(Spinner spinner, String myString) {
for(int i = 0; i < spinner.getCount(); i++){
if(spinner.getItemAtPosition(i).toString().equals(myString)){
spinner.setSelection(i);
break;
}
}
}
private void validateInput() {
boolean hasName = nameEditText.length() > 0;
boolean hasCpf = cpfEditText.isValid();
boolean hasEmail = emailEditText.length() > 0 && emailEditText.isValid();
boolean hasPhoneCode = dddEditText.length() > 0;
boolean hasPhone = phoneNumberEditText.length() > 0;
boolean hasBirthday = birthdayEditText.isValidDate();
boolean hasStreet = addressEditText.length() > 0;
boolean hasNumber = numberEditText.length() > 0;
boolean hasNeighborhood = neighborhoodEditText.length() > 0;
boolean hasCity = cityEditText.length() > 0;
boolean hasState = ((String) stateSpinner.getSelectedItem()).length() > 0;
if (hasName && hasCpf && hasEmail && hasPhone && hasPhoneCode && hasBirthday && hasStreet &&
hasNumber && hasNeighborhood && hasCity && hasState) {
saveButton.setEnabled(true);
} else {
saveButton.setEnabled(false);
}
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void editPicture(View view) {
Intent intent = new Intent(ChangeRegisterActivity.this, RegisterPhotoActivity.class);
intent.putExtra(RegisterPhotoActivity.CHANGE_PICTURE_EXTRA, true);
startActivity(intent);
}
public void save(View view) {
User user = buildUser();
if(NetworkUtils.hasInternetConnection(this)) {
new RegisterAsyncTask().execute(user.toJsonString());
} else {
Toast.makeText(this, R.string.no_internet_msg, Toast.LENGTH_LONG).show();
}
}
private User buildUser() {
User user = new User();
user.setName(nameEditText.getText().toString());
user.setCpf(cpfEditText.getText().toString());
user.setEmail(emailEditText.getText().toString());
user.setPhoneCode(dddEditText.getText().toString());
user.setPhone(phoneNumberEditText.getText().toString());
user.setBirthday(birthdayEditText.getText().toString());
user.setAddressStreet(addressEditText.getText().toString());
user.setAddressNumber(numberEditText.getText().toString());
user.setAddressNeighborhood(neighborhoodEditText.getText().toString());
user.setAddressCity(cityEditText.getText().toString());
user.setAddressState((String) stateSpinner.getSelectedItem());
return user;
}
private class RegisterAsyncTask extends AsyncTask {
@Override
protected String doInBackground(String... strings) {
String userJsonObject = strings[0];
try {
URL url = NetworkUtils.getUserRegisterURL();
return NetworkUtils.doPost(url, userJsonObject);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException while trying to register the user", e);
}
return null;
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if(TextUtils.isEmpty(response)) {
Toast.makeText(ChangeRegisterActivity.this, R.string.change_register_error_msg, Toast.LENGTH_LONG).show();
return;
}
try {
JSONObject responseJson = new JSONObject(response);
int status = responseJson.getInt("status");
if(status == 200) {
User user = new User(responseJson.getJSONObject("user"));
Preferences.saveUser(ChangeRegisterActivity.this, user);
ChangeRegisterActivity.this.finish();
} else {
// TODO show dialog handling error
Toast.makeText(ChangeRegisterActivity.this, responseJson.getString("message"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Log.e(TAG, "JSONException while parsing change register response", e);
FirebaseCrash.report(e);
}
}
}
}*

activivity_change_register.xml

android:id="@+id/birthday_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/phone_number_input"
android:layout_marginTop="8dp"
>
android:id="@+id/birthday"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="@string/birthday_hint"
android:inputType="text"
android:maxLines="1"
android:nextFocusForward="@id/address_input"
/>

Seja o primeiro a responder

Artigos recomendados