gpt4 book ai didi

java - 我需要有关阅读来自 SQLite 的已签名电子邮件的帮助

转载 作者:搜寻专家 更新时间:2023-10-30 23:28:18 24 4
gpt4 key购买 nike

我希望我在欢迎 Activity 中的 TextView 能够捕获在登录 Activity 中输入的电子邮件。

我想为我尝试输入的每个用户检索该电子邮件,现在我只能检索最后注册的用户电子邮件,无论我登录的其他用户是什么,你知道如何做到这一点吗?

我有一个 WelcomeActivity 和一个 SignInActivity,我想做的是 WelcomeActivity 有一个 TextView,我想让 TextView 在我签名时从 SQLiteHelper 中读取使用保存在数据库中的电子邮件和密码,它应该读取用于使用 SignInActivity 登录的选定电子邮件,很容易使用电子邮件的 EditText 中的 putExtra 和 getExtra 并通过 TextView 获取它,但我想要用户是唯一的,我不希望用户进入只显示名称的页面,如果我要检索检索到的用于登录的电子邮件会更合理。

DataBAseHelper.java

public class DataBaseHelper extends SQLiteOpenHelper
{
public static final String DB_NAME = "users.dbHelper";
public static final int DB_VERSION = 1;
public static final String TABLE_USERS = "users";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String[] ALL_COLUMNS = {COLUMN_EMAIL, COLUMN_PASSWORD};
public DataBaseHelper dbHelper;
public static SQLiteDatabase SQLiteDatabase;

public static final String SQL_CREATE =
"CREATE TABLE IF NOT EXISTS " + TABLE_USERS + " (" +
COLUMN_EMAIL + " STRING PRIMARY KEY, " +
COLUMN_PASSWORD + " STRING);";

public static final String SQL_DROP = "DROP TABLE " + TABLE_USERS;

public DataBaseHelper(@Nullable Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(SQL_DROP);
onCreate(db);
}

//---opens the database---
public DataBaseHelper open() throws SQLException
{

SQLiteDatabase = this.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
if (SQLiteDatabase != null)
SQLiteDatabase.close();
if (dbHelper != null)
dbHelper.close();
}

// Checking if email exists
public boolean checkEmail(String email)
{
SQLiteDatabase = this.getWritableDatabase();
Cursor cursor = SQLiteDatabase.rawQuery("SELECT * FROM " + TABLE_USERS + " WHERE "
+ COLUMN_EMAIL + " =? ", new String[]{email});
if (cursor.getCount() > 0) return false;
else return true;
}

// Checking the email and password
public boolean checkEmailPassword(String email, String password)
{
SQLiteDatabase = this.getWritableDatabase();
Cursor cursor = SQLiteDatabase.rawQuery("SELECT * FROM " + TABLE_USERS + " WHERE "
+ COLUMN_EMAIL + " =? " + " AND " + COLUMN_PASSWORD + " =? "
, new String[]{email, password});
if (cursor.getCount() > 0) return true;
else return false;
}

public boolean insertEntry(String email, String password)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("EMAIL", email);
newValues.put("PASSWORD", password);

// Insert the row into your table
long ins = SQLiteDatabase.insert(TABLE_USERS, null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
if (ins == -1) return false;
else return true;
}

public int deleteEntry(String email)
{
//String id=String.valueOf(ID);
String where = "EMAIL=?";
int numberOFEntriesDeleted = SQLiteDatabase.delete(TABLE_USERS, where, new String[]{email});
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}

public String getSingleEntry(String email)
{
Cursor cursor = SQLiteDatabase.query(TABLE_USERS, null, " EMAIL=?", new String[]{email}, null, null, null);
if (cursor.getCount() < 1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}

public void updateEntry(String email, String password)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("EMAIL", email);
updatedValues.put("PASSWORD", password);

String where = "EMAIL = ?";
SQLiteDatabase.update(TABLE_USERS, updatedValues, where, new String[]{email});
}

public String getUsername(String COLUMN_EMAIL) throws SQLException
{
String email = "No Email Found";
SQLiteDatabase = this.getReadableDatabase();
Cursor cursor = SQLiteDatabase.query(TABLE_USERS, new String[]{COLUMN_EMAIL}, null, null,
null, null, null);
if (cursor.moveToNext()) {
cursor.moveToNext();
cursor.moveToLast();

email = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
}
cursor.close();

return email;
}

WelcomeActivity.java

public class WelcomeActivity extends AppCompatActivity
{

private static int SPLASH_TIME_OUT = 4000;
private SQLiteDatabase m_db;
private TextView tvEmails;
DataBaseHelper db = new DataBaseHelper(this);

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
db = new DataBaseHelper(this);
try {
db = db.open();
} catch (android.database.SQLException e) {
e.printStackTrace();
}

TextView tvEmail = (TextView) findViewById(R.id.tvEmails);

// Bundle bundle;
// bundle = getIntent().getExtras();
// tvEmail.setText(bundle.getString(""));

String email = db.getUsername(DataBaseHelper.COLUMN_EMAIL);

tvEmail.setText(email);

new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(WelcomeActivity.this, EthicsActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}

LoginActivity.java

public class LoginActivity extends AppCompatActivity
{

public static final String PREFS = "global_prefs";// UI References
public static final String KEY_EMAIL = "email";

private DataBaseHelper db;
private EditText etEmail, etPassword;
private Button btnSignIn;
private TextView tvRegisterClick;

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

final EditText etEmail = findViewById(R.id.etEmail);
final EditText etPassword = findViewById(R.id.etPassword);
Button btnSignIn = findViewById(R.id.btnSignIn);
TextView tvRegisterClick = findViewById(R.id.tvRegisterClick);

db = new DataBaseHelper(this);

tvRegisterClick.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});

btnSignIn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// Store values at the same time of the login attempt

String email = etEmail.getText().toString();
String password = etPassword.getText().toString();

boolean checkEmailPassword = db.checkEmailPassword(email, password);
if (checkEmailPassword) {
Toast.makeText(getApplicationContext(), "login successfully!", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
intent.putExtra("", email);
startActivity(intent);

} else {
if (!checkEmailPassword)
Toast.makeText(getApplicationContext(), "Wrong email or password", Toast.LENGTH_SHORT).show();
}

//// Reset Errors
// etEmail.setError(null);
// etPassword.setError(null);
//
// boolean errorOccured = false;
// View focusView = null;
//
// // Check for a valid password if the user entered one
// if(!isPasswordValid(password))
// {
// etPassword.setError("Invalid password");
// focusView = etPassword;
// errorOccured = true;
// }
//
// // Check for a valid email, if the user enters one
// if(!isEmailValid(email))
// {
// etEmail.setError("Invalid Email");
// focusView = etEmail;
// errorOccured = true;
// }
//
// if(errorOccured)
// {
// focusView.requestFocus();
// }
// else
// {
// SharedPreferences.Editor editor = getSharedPreferences(PREFS, MODE_PRIVATE).edit();
//
// editor.putString(KEY_EMAIL, email);
// editor.apply();
//
// finish();
// }
}
// private boolean isEmailValid(String email)
// {
// return email.contains("");
// }
//
// private boolean isPasswordValid(String password)
// {
// return password.length() > 4;
// }
});
}
}

最佳答案

因为您在启动 WelcomeActivity 之前实际上已成功登录,因此无需根据您从数据库中获得的电子邮件从数据库中获取电子邮件(听起来很愚蠢/无用).但是,您需要做的是将电子邮件从登录 Activity 传递到欢迎 Activity 。这就是您的问题所在。

您遇到的第一个问题是,使用 intent.putExtra("", email); 没有有用的标识符,无法在尝试提取 Extra 时确定它。相反,您应该使用(存在 KEY_EMAIL):-

intent.putExtra(KEY_EMAIL, email); //<<<<<<<<<< use a key for the extra so it can be obtained

第二个问题是检索传递的电子邮件。可以在欢迎 Activity 中检索电子邮件,例如使用:-

    TextView tvEmail = (TextView) findViewById(R.id.tvEmails);

// Just get the email from the Intent
Intent passed_intent = getIntent(); //<<<<<<<<<< ADDED
tvEmail.setText(passed_intent.getStringExtra(LoginActivity.KEY_EMAIL)); //<<<<<<<<<< ADDED

关于java - 我需要有关阅读来自 SQLite 的已签名电子邮件的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53266862/

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