关于Android Studio和SQlite

时间:2016-01-30 10:03:58

标签: android sqlite

我有2个问题想问一下android studio和sqlite。

1)我正在尝试在模拟器或手机中运行我的项目。当我想通过按下返回按钮(手机的返回功能)返回到上一个活动但它关闭了我的所有项目。但是我看到我的朋友可以通过按下返回按钮返回到之前的活动。我知道怎么回事?为什么?

2)我已经为我的项目完成了更新功能。情况是当userA进入“查看个人资料”活动并点击编辑信息时,另一个名为“updateinfo”的活动将会出来。然后用户通过点击更新他的信息。更新按钮。它已成功更新并返回“查看个人资料”活动以查看其更新的个人资料。

但我遇到的问题是它没有显示更新的信息。它只显示一个空白的“查看个人资料”活动,没有任何更新或未更新的信息。

我该怎么办?

这里是我的数据库更新功能

     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;
   }

这是我的updateinfo活动功能

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();
                }
            }
        }
    });
}

这是我的viewprofile活动函数

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();

        }
    });
}

2 个答案:

答案 0 :(得分:1)

  1. 您始终开始新活动并完成最后一项活动
  2. startActivity(i); finish();

    如果您想稍后返回,请不要完成它,您可以使用finish()返回上一个活动;或者按回电话

    1. 你需要在更新成功时调用finish()(你需要先实现答案1)
    2. Toast.makeText(getBaseContext(),“Update Success”,Toast.LENGTH_LONG)。show();

      Intent i = new Intent(EditProfile.this,MyProfile.class);

      startActivity(ⅰ);

      结束();

      MyProfile中的

      使用户名变量全局

      String username;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      ....
          username = i.getStringExtra("username");
      ....
      }
      

      最后,此代码需要位于 onResume()

      etShow.setText(username);
      profileInfo = db.getAllinfo(username);
      etName.setText(profileInfo.get_name());
      etEmail.setText(profileInfo.get_email());
      etPhone.setText(profileInfo.get_phone());
      

答案 1 :(得分:1)

公共布尔更新(editProfile.EUsers eusers){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentvalues = new ContentValues();

    String username = eusers.getUsername();
    String password = eusers.getPassword();
    int id = eusers.getId();
    contentvalues.put(editProfile.EUsers.COL3_PASSWORD,password);
    contentvalues.put(editProfile.EUsers.COL2_USERNAME,username);

    int res =db.update(editProfile.EUsers.TABLE_NAME,values,editProfile.EUsers.COL1_ID+" = ?",new String[]{String.valueOf(id)});

    if(res >0)
        return true;

    return false;
}