Guest User

Untitled

a guest
Oct 14th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package com.bla.androidsqlitebasics.activity;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. import com.bla.androidsqlitebasics.database.DBHelper;
  12. import com.live.sinhalacoder.androidsqlitebasics.R;
  13.  
  14. public class HomeActivity extends AppCompatActivity {
  15.  
  16. EditText usernameEt, passwordEt;
  17. Button loginBt, registerBt;
  18.  
  19. //to get access to database table
  20. DBHelper mHelper;
  21.  
  22. //newly added user primary key
  23. long userId = -1;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_home);
  29.  
  30. usernameEt = findViewById(R.id.idUsernameEt);
  31. passwordEt = findViewById(R.id.idPasswordEt);
  32.  
  33. loginBt = findViewById(R.id.idLoginBt);
  34. registerBt = findViewById(R.id.idRegisterBt);
  35.  
  36. //initialize db helper when app create
  37. mHelper = new DBHelper(this);
  38.  
  39. //if user clicked login button
  40. loginBt.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. //login existing user
  44. login();
  45. }
  46. });
  47.  
  48. //if user clicked register button
  49. registerBt.setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View v) {
  52. //add new user to the database
  53. registerUser();
  54. }
  55. });
  56. }
  57.  
  58. private void login() {
  59. String username = usernameEt.getText().toString();
  60. String password = passwordEt.getText().toString();
  61.  
  62. //if username and pass does not match -1 will return from checkUser function and if not -1 logged in
  63. userId = mHelper.checkUser(username, password);
  64. if (userId != -1) {
  65. Intent intent = new Intent(HomeActivity.this, ProfileManagementActivity.class);
  66. intent.putExtra("userId", userId);
  67. startActivity(intent);
  68. } else {
  69. Toast.makeText(this, "Err.. Try again!", Toast.LENGTH_SHORT).show();
  70. }
  71. }
  72.  
  73. public void registerUser() {
  74. String username = usernameEt.getText().toString();
  75. String password = passwordEt.getText().toString();
  76.  
  77. userId = mHelper.addInfo(username, password);
  78. if (userId == -1) {
  79. Toast.makeText(this, "Err.. Try again!", Toast.LENGTH_SHORT).show();
  80. } else {
  81. //Toast.makeText(this, "Successfully registed!", Toast.LENGTH_SHORT).show();
  82. login();
  83. }
  84. }
  85.  
  86. @Override
  87. protected void onDestroy() {
  88. super.onDestroy();
  89. mHelper.close();
  90. }
  91. }
Add Comment
Please, Sign In to add comment