Here is the full code. The app is working perfectly but the only thing where I am facing problem is that ---> 1. When the user registers for the first time, the app registers properly but when I close the app and open the app it still shows the registration window where as it show show the Welcome window. 2. I have to enter the details again and again when I login where as it should do only one time login.. it should not ask again and again.
These are the problems. Please anyone help me to solve this problem.
the MainActivity code
public class MainActivity extends AppCompatActivity {
SQLiteOpenHelper openHelper;
SQLiteDatabase db;
Button _btnreg, _btnlogin;
EditText _txtfname, _txtlname, _txtpass, _txtemail, _txtphone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openHelper = new DatabaseHelper(this);
_txtfname = (EditText)findViewById(R.id.txtfname);
_txtlname = (EditText)findViewById(R.id.txtlname);
_txtpass = (EditText)findViewById(R.id.txtpass);
_txtemail = (EditText)findViewById(R.id.txtemail);
_txtphone = (EditText)findViewById(R.id.txtphone);
_btnlogin=(Button)findViewById(R.id.btnlogin);
_btnreg=(Button)findViewById(R.id.btnreg);
Log.d("MainActivity","40");
_btnreg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity","50");
db=openHelper.getWritableDatabase();
String fname=_txtfname.getText().toString();
String lname=_txtlname.getText().toString();
String pass=_txtpass.getText().toString();
String email=_txtemail.getText().toString();
String phone=_txtphone.getText().toString();
insertdata(fname, lname,pass,email,phone);
Toast.makeText(getApplicationContext(), "register successfully",Toast.LENGTH_LONG).show();
Log.d("MainActivity","60");
}
});
_btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity","70");
Intent intent = new Intent(MainActivity.this, login.class);
Log.d("MainActivity","80");
startActivity(intent);
Log.d("MainActivity","+90");
}
});
}
public void insertdata(String fname, String lname, String pass, String email, String phone) {
Log.d("MainActivity","100");
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_2, fname);
contentValues.put(DatabaseHelper.COL_3, lname);
contentValues.put(DatabaseHelper.COL_4, pass);
contentValues.put(DatabaseHelper.COL_5, email);
contentValues.put(DatabaseHelper.COL_6, phone);
long id = db.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
Log.d("MainActivity","200");
}
}
The databaseHelper code
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="register.db";
public static final String TABLE_NAME="registeration";
public static final String COL_1="ID";
public static final String COL_2="FirstName";
public static final String COL_3="LastName";
public static final String COL_4="Password";
public static final String COL_5="Email";
public static final String COL_6="Phone";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("DatabaseHelper","10");
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,FirstName TEXT,LastName TEXT,Password TEXT,Email TEXT,Phone TEXT)");
Log.d("DatabaseHelper","20");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("DatabaseHelper","30");
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
Log.d("DatabaseHelper","40");
onCreate(db);
}
}
here is the Login Activity code
public class login extends AppCompatActivity {
SQLiteDatabase db;
SQLiteOpenHelper openHelper;
Button __btnLogin;
EditText __txtEmail,__txtPass;
Cursor cursor;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d("login","11");
openHelper = new DatabaseHelper(this);
db=openHelper.getReadableDatabase();
__btnLogin = (Button)findViewById(R.id.btnLogins);
__txtEmail = (EditText)findViewById(R.id.txtEmails);
__txtPass = (EditText)findViewById(R.id.txtPasss);
Log.d("login","22");
__btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = __txtEmail.getText().toString();
String pass = __txtPass.getText().toString();
if (pass == "" || email == "") {
Toast.makeText(getApplicationContext(),"No Entry", Toast.LENGTH_LONG).show();
}
Log.d("login","33");
cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_5 + " =? AND " + DatabaseHelper.COL_4 + " =? ", new String[]{email,pass});
Log.d("login","44");
if(cursor!=null) {
Log.d("login","55");
if (cursor.getCount()>0) {
Log.d("login","66");
//cursor.moveToNext();
Log.d("login","77");
startActivity(new Intent(login.this, Welcome.class));
Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
}
else {
Log.d("login","88");
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
Log.d("login","99");
}
}
}
});
}
}
THANK YOU