Reputation: 67
I want my text view in welcome activity to capture the email that was entered in the sign in activity.
I want to retrieve that for every user I am trying to enter, right now I can only retrieve the last registered user email no matter what other users I am signing in with, do you have an idea how to do that?
I have a WelcomeActivity
and a SignInActivity
, what I want to do is that WelcomeActivity have a TextView, I want to make TextView read from the SQLiteHelper that when I sign in with an email and password that are saved in the database it should read the selected email that was used to sign in using SignInActivity, it is easy to use putExtra and getExtra from the EditText for Email and get it thru the TextView, but I want the user to be unique, I don't want the user to enter the page displaying just the name, it would be more reasonable if I were to retrieve that email that was retrieved to Sign in with.
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;
// }
});
}
}
Upvotes: 1
Views: 74
Reputation: 57043
As you've actually signed in successfully before starting WelcomeActivity, there is no need to get the email from the database based upon the email that you have got from the database (meant to sound stupid/futile). However, what you need to do is pass the email from the Login activity to the Welcome activity. Which is where your issue appears to lie.
The first issue you have is that using intent.putExtra("", email);
does not have a useful identifier to enable the Extra to be determined when trying to extract it. Instead you should be using (KEY_EMAIL exists):-
intent.putExtra(KEY_EMAIL, email); //<<<<<<<<<< use a key for the extra so it can be obtained
The second issue is retrieving the passed email. The email can be retrieved in the Welcome Activity e.g. using :-
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
Upvotes: 1