-1

Helper1 Helper2 Home Activity(Here I want to show username) Main1 Main2Display username from SQLite database after login in textView

I am inserting data via SQL query using SQLite database. Fields are Name, Email, Password. And I want to print Name on the text view on my Dashboard activity. Login is going via Email and password. After login, I want to print the Name of the user who is log in.

"INSERT INTO "+SQLiteHelper.TABLE_NAME+" (name,email,password) VALUES('tarun', 'tarun@gmail.com', '12345');

I have 2 activity Main which is login and dashboard and helper class. I have only two text box on Main activity for login named email and password.

Tarun Sharma
  • 914
  • 9
  • 15

1 Answers1

0

Write the below method in DatabaseHelper class.

public Cursor getData(String email, String password) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from " + Table_Name + " where " + DatabaseHelper.Column2 + "=?" + " and " + DatabaseHelper.Column3 + "=?", new String[] {email, password});
    return res;
}

Put this code in MainActivity login button click

Cursor rs = databaseHelper.getData(editTextEmail.getText().toString(), editTextPassword.getText().toString());

if(rs.moveToFirst()) {
    String name = rs.getString(rs.getColumnIndex(databaseHelper.Column1));
    String email = rs.getString(rs.getColumnIndex(databaseHelper.Column2));

    Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();

    Intent intentHome = new Intent(MainActivity.this, Home.class);
    intentHome.putExtra("key_name", name);
    intentHome.putExtra("key_email", email);
    startActivity(intentHome);

    if (rs != null && !rs.isClosed())  {
       rs.close();
    }
} else {
    Toast.makeText(MainActivity.this, "Invalid login", Toast.LENGTH_SHORT).show();
}

As you have passed name and email from MainActivity to Home activity via Intent so call the below code in HomeActivity.

//display name or email as 
Name.setText(getIntent().getStringExtra("key_name"));
Usermail.setText(getIntent().getStringExtra("key_email"));
Hari N Jha
  • 484
  • 1
  • 3
  • 11
  • Hello, can you help me I am so confused? – Tarun Sharma Jul 28 '19 at 12:17
  • 1
    I have posted my answer for your help only. Follow and let me know where are you confused. If nothing is clear to you then I will suggest you to go with SqLite tutorial first to understand the basic components. – Hari N Jha Jul 29 '19 at 03:41
  • Data is inserted successfully, login in also done successfully email id and username I want to show on homepage, Email id I print via intent it shows successfully but I stuck in showing the username who is log in. I can show you code but after posting code here spacing error and many more errors related to formatting. – Tarun Sharma Jul 29 '19 at 05:33
  • I added my code via images please check and help me. Thanks – Tarun Sharma Jul 29 '19 at 05:58
  • I have gone through the images, I hope you can insert the record in DB successfully. You want to check for login with email and password and display the user's name and email to Home activity. Please confirm. – Hari N Jha Jul 29 '19 at 10:18
  • Hearty thank you so much, sir, finally it works :-). – Tarun Sharma Jul 30 '19 at 07:06
  • Sir Can you help me? – Tarun Sharma Sep 24 '19 at 12:19
  • https://stackoverflow.com/questions/58079712/how-can-i-populate-dynamic-markers-on-map-using-sqlite-database-databae – Tarun Sharma Sep 24 '19 at 12:20