samedi 30 janvier 2016

about update data in Android Studio and SQlite

I have 2 questions want to ask about android studio and sqlite.

1) I am trying to run my project in emulator or phone. When I want to return to previous activity by pressing the return button(return function for phone) but it close all my project.But I saw my friend's can return to previous activity by pressing the return button.May I know how and why??

2) I had done update function for my project.The situation is when userA go to "view profile" activity and click edit info, another activity that call "updateinfo" will come out.Then after userA update his information by clicking update button.It's successful update and go back to "view profile" activity to see his updated profile.

But the problem I faced is it does not show out the updated information.It just show a blank "view profile" activity without any information that updated or haven updated.

What should I do?

here my database update function

     public boolean updateProfile(String username, String password, String email, String phone)
   {
       SQLiteDatabase db = this.getWritableDatabase();
       ContentValues values = new ContentValues();
       values.put (COL_2,username);
       values.put(COL_3,password);
       values.put(COL_4,email);
       values.put(COL_5,phone);

      db.update(Table_NAME,values,COL_2 + "=?",new String[]{username});
       db.close();
       return true;
   }

here is my updateinfo activity function

public class EditProfile extends AppCompatActivity {
EditText etEmail,etPhone,etPassword,etConPassword,etUsername;
String password,conpassword,Email,Phone;
Button bUpdate;
DatabaseOperations DB = new DatabaseOperations(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_profile);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPhone = (EditText) findViewById(R.id.etPhone);
    etPassword = (EditText) findViewById(R.id.etPassword);
    etConPassword = (EditText) findViewById(R.id.etConPassword);
    etUsername = (EditText) findViewById(R.id.etUsername);
    bUpdate = (Button) findViewById(R.id.bUpdate);

    Intent i = getIntent();
    String email = i.getStringExtra("email");
    etEmail.setText(email);
    String phone = i.getStringExtra("phone");
    etPhone.setText(phone);
    String username = i.getStringExtra("username");
    etUsername.setText(username);

    bUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            password = etPassword.getText().toString();
            conpassword = etConPassword.getText().toString();
            Email = etEmail.getText().toString();
            Phone = etPhone.getText().toString();

            if (!(password.equals(conpassword))) {
                Toast.makeText(getBaseContext(), "Passwords are not matching", Toast.LENGTH_LONG).show();
                etPassword.setText("");
                etConPassword.setText("");
                etEmail.setText("");
                etPhone.setText("");
            } else if (etPassword.length() == 0 || etConPassword.length() == 0 || etEmail.length() == 0 || etPhone.length() == 0) {
                etPassword.setError("Please complete all information");
                etConPassword.setError("Please complete all information");
                etEmail.setError("Please complete all information");
                etPhone.setError("Please complete all information");
            } else if (etPassword.length() < 6) {
                etPassword.requestFocus();
                etPassword.setError("Password at least 6 characters");
                etPassword.setText("");
                etConPassword.setText("");
                etEmail.setText("");
                etPhone.setText("");
            } else {
                boolean isUpdate = DB.updateProfile(etUsername.getText().toString(),etPassword.getText().toString(),etEmail.getText().toString(),etPhone.getText().toString());
                if(isUpdate == true) {
                    Toast.makeText(getBaseContext(), "Update Success", Toast.LENGTH_LONG).show();
                    Intent i = new Intent(EditProfile.this, MyProfile.class);
                    startActivity(i);
                    finish();
                }
                else
                {
                    Toast.makeText(getBaseContext(), "Data Not Updated", Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}

and here is my viewprofile activity function

public class MyProfile extends AppCompatActivity {
EditText etName,etEmail,etPhone,etShow;
Button bEdit;
String fullname,email,phone;
DatabaseOperations db = new DatabaseOperations(this);
PersonalData profileInfo;

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



    etName = (EditText) findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPhone = (EditText) findViewById(R.id.etPhone);
    bEdit = (Button) findViewById(R.id.bEdit);
    etShow = (EditText) findViewById(R.id.etShow);


    fullname = etName.getText().toString();
    email = etEmail.getText().toString();
    phone = etPhone.getText().toString();

    Intent i = getIntent();
    String username = i.getStringExtra("username");
    etShow.setText(username);
    profileInfo = db.getAllinfo(username);
    etName.setText(profileInfo.get_name());
    etEmail.setText(profileInfo.get_email());
    etPhone.setText(profileInfo.get_phone());


    bEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MyProfile.this,EditProfile.class);
            i.putExtra("email", etEmail.getText().toString());;
            i.putExtra("phone", etPhone.getText().toString());;
            i.putExtra("username", etShow.getText().toString());
            startActivity(i);
            finish();

        }
    });
}

Aucun commentaire:

Enregistrer un commentaire