gpt4 book ai didi

android - 关于在 Android Studio 和 SQlite 中更新数据

转载 作者:行者123 更新时间:2023-11-29 20:10:19 24 4
gpt4 key购买 nike

关于android studio和sqlite我有2个问题想问。

1) 我正在尝试在模拟器或手机中运行我的项目。当我想通过按返回按钮(手机返回功能)返回到之前的 Activity 时,它关闭了我所有的项目。但是我看到我的 friend 可以通过按下返回按钮返回到之前的 Activity 。我可以知道如何以及为什么吗?

2)我已经为我的项目完成了更新功能。情况是当用户A转到“查看个人资料” Activity 并单击编辑信息时,另一个调用“updateinfo”的 Activity 将会出现。然后在用户A通过单击更新他的信息后更新按钮。更新成功并返回“查看个人资料” Activity 以查看他更新后的个人资料。

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

我该怎么办?

这里是我的数据库更新函数

     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 Activity 函数

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 Activity 函数

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

}
});
}

最佳答案

  1. 你总是开始新的 Activity 并完成最后的 Activity

开始 Activity (我);
结束();

如果您想稍后返回,请不要完成它,您可以使用 finish() 返回上一个 Activity ;或在手机中按回键

  1. 你只需要在更新成功时调用finish()(你需要先实现答案1)

Toast.makeText(getBaseContext(), "更新成功", Toast.LENGTH_LONG).show();

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

开始 Activity (i);

完成();

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

关于android - 关于在 Android Studio 和 SQlite 中更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com