Shrung ßhatt Shrung ßhatt - 14 days ago 11
Android Question

User sign up and login, storing database and querying for username and password using SQLite

I am a newbie in Android. So my application requires a user access, So I have already set up the layout files.

I have created the Database Schema, created an class that extends SQliteOpenHelper, assigned the contentValues,and also I have also made a query to retrieve the username and password.

Logging the messages The database gets created, the new rows are also inserted into the database but when I use the query to fetch the user in login screen its malfunctioning.

So to sum it up my query is troubling me.

SQliteOpenHeler Class

package com.example.andorid.ersnexus.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import com.example.andorid.ersnexus.Message;
import com.example.andorid.ersnexus.database.UserDbSchema.UserTable;


public class UserBaseHelper extends SQLiteOpenHelper {

private static final int VERSION = 1;
private static final String DATABASE_NAME = "userBase.db";


private SQLiteDatabase mDatabase;
private Context context;

public UserBaseHelper (Context context) {
super(context, DATABASE_NAME, null, VERSION);
this.context = context;
Message.message("Constructor Called", context);
}


@Override
public void onCreate (SQLiteDatabase db) {
db.execSQL("create table " + UserTable.NAME + "(" +
" _id integer primary key autoincrement, " +
UserTable.Cols.UUID + ", " +
UserTable.Cols.ENROLLMENT_NUMBER + ", " +
UserTable.Cols.USER_NAME + ", " +
UserTable.Cols.FULL_NAME + ", " +
UserTable.Cols.PASSWORD + ", " +
UserTable.Cols.EMAIL + "," +
UserTable.Cols.DATE_OF_BIRTH +
")"
);
Message.message("onCreate() called", context);

}


public String fetchUser (String userName) {

mDatabase = this.getReadableDatabase();


UserCursorWrapper c = queryCrimes(new String[]{UserTable.Cols.USER_NAME, UserTable.Cols.PASSWORD},
UserTable.Cols.USER_NAME + " = ?", new String[]{userName});


String uname,pass;
pass = "not found";
try {
c.moveToFirst();
while (!c.isAfterLast()) {
uname = c.getString(c.getColumnIndex(UserTable.Cols.USER_NAME));
if(uname.equals(userName)){
pass = c.getString(c.getColumnIndex(UserTable.Cols.PASSWORD));
}
c.moveToNext();
}
}finally{
c.close();
}
return pass;
}

@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS " + UserTable.NAME;
db.execSQL(query);
Message.message("Database Upgraded", context);
this.onCreate(db);

}

private UserCursorWrapper queryCrimes (String[] projection, String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(
UserTable.NAME,
projection,//Columns - null select columns
whereClause,
whereArgs,
null,//groupBy
null,//having
null //orderBy
);
return new UserCursorWrapper(cursor);
}


}


The Working

When the user hits Submit button in sign up activity the contentValues are stored using a method addUser(UserData userdata)

public void addUser (UserData userData) {

ContentValues values = getContentValues(userData);
mDatabase.insert(UserTable.NAME, null, values);
Message.message("Row inserted",mContext);


}

public static ContentValues getContentValues (UserData userData) {
ContentValues values = new ContentValues();

values.put(UserTable.Cols.UUID, userData.getId().toString());
values.put(UserTable.Cols.ENROLLMENT_NUMBER, userData.getEnrollmentNumber());
values.put(UserTable.Cols.USER_NAME, userData.getUserName());
values.put(UserTable.Cols.FULL_NAME, userData.getFullName());
values.put(UserTable.Cols.PASSWORD, userData.getPassword());
values.put(UserTable.Cols.EMAIL, userData.getEmail());
values.put(UserTable.Cols.DATE_OF_BIRTH, userData.getDob().toString());

return values;
}


Now in the login activity when the user hits login button the following query is used

public String fetchUser (String userName) {

mDatabase = this.getReadableDatabase();


UserCursorWrapper c = queryCrimes(new String[]{UserTable.Cols.USER_NAME, UserTable.Cols.PASSWORD},
UserTable.Cols.USER_NAME + " = ?", new String[]{userName});


String uname,pass;
pass = "not found";
try {
c.moveToFirst();
while (!c.isAfterLast()) {
uname = c.getString(c.getColumnIndex(UserTable.Cols.USER_NAME));
if(uname.equals(userName)){
pass = c.getString(c.getColumnIndex(UserTable.Cols.PASSWORD));
}
c.moveToNext();
}
}finally{
c.close();
}
return pass;
}

private UserCursorWrapper queryCrimes (String[] projection, String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(
UserTable.NAME,
projection,//Columns - null select columns
whereClause,
whereArgs,
null,//groupBy
null,//having
null //orderBy
);
return new UserCursorWrapper(cursor);
}


And here is what it takes in the login activity to get the username from fetchUser(userName) method

package com.example.andorid.ersnexus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.andorid.ersnexus.database.UserBaseHelper;





public class UserLoginActivity extends AppCompatActivity {

private EditText mUserName;
private EditText mUserPassword;
private Button mLoginButton;
private Button mSignUpButton;
private UserBaseHelper mHelper;
private String userName;
private String pass;
private String password;

@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_user_login);

mHelper = new UserBaseHelper(this);


//user UserName editText in fragment_user_login
mUserName = (EditText) findViewById(R.id.login_user_name);



//PASSWORD editText
mUserPassword = (EditText) findViewById(R.id.login_user_pass);




//SignUp button
mSignUpButton = (Button) findViewById(R.id.sign_up_button);
mSignUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
Intent i = new Intent(UserLoginActivity.this, UserSignUpActivity.class);
startActivity(i);

}
});



//Login Button
mLoginButton = (Button) findViewById(R.id.login_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
userName = mUserName.getText().toString();
pass = mUserPassword.getText().toString();
password = mHelper.fetchUser(userName);

if (pass.equals(password)) {
Intent i = new Intent(UserLoginActivity.this, UserProfileActivity.class);
i.putExtra("username", userName);
startActivity(i);
}
else{
Toast.makeText(UserLoginActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.test_textView);
textView.setText(password);
}

}
});
}

}


UserSignUpActivity

package com.example.andorid.ersnexus;

import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.andorid.ersnexus.database.UserBaseHelper;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;


public class UserSignUpFragment extends Fragment {


private static final int REQUEST_DATE = 0;
private static final String DIALOG_DATE = "dialog_date";
private EditText mErNo1;
private EditText mErNo2;
private EditText mErNo3;
private EditText mErNo4;
private EditText mErNo5;
private EditText mErNo6;
private EditText mErNo7;
private EditText mErNo8;
private EditText mErNo9;
private EditText mErNo10;
private EditText mErNo11;
private EditText mErNo12;
private EditText mUserName;
private EditText mFullName;
private EditText mPassword;
private EditText mEmail;
private EditText mConfirmPassword;
private Button mDob;
private Button mSubmit;
private UserData mUserData;
private String erNo1;
private String erNo2;
private String erNo3;
private String erNo4;
private String erNo5;
private String erNo6;
private String erNo7;
private String erNo8;
private String erNo9;
private String erNo10;
private String erNo11;
private String erNo12;
private UserBaseHelper mHelper;

DateFormat formatDate = DateFormat.getDateInstance();


@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserData = new UserData();
mHelper = new UserBaseHelper(getActivity());
}


public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user_sign_up, container, false);


//enrollmentNumber of user


mErNo1 = (EditText) v.findViewById(R.id.erno_1);
mErNo1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo1(s.toString());
if (mErNo1.getText().length() == 1) {
mErNo2.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo1 = mErNo1.getText().toString();

mErNo2 = (EditText) v.findViewById(R.id.erno_2);
mErNo2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo2(s.toString());
if (mErNo2.getText().length() == 1) {
mErNo3.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo2 = mErNo2.getText().toString();

mErNo3 = (EditText) v.findViewById(R.id.erno_3);
mErNo3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo3(s.toString());
if (mErNo3.getText().length() == 1) {
mErNo4.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo3 = mErNo3.getText().toString();

mErNo4 = (EditText) v.findViewById(R.id.erno_4);
mErNo4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo4(s.toString());
if (mErNo4.getText().length() == 1) {
mErNo5.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo4 = mErNo4.getText().toString();


mErNo5 = (EditText) v.findViewById(R.id.erno_5);
mErNo5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo5(s.toString());
if (mErNo5.getText().length() == 1) {
mErNo6.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo5 = mErNo5.getText().toString();


mErNo6 = (EditText) v.findViewById(R.id.erno_6);
mErNo6.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo6(s.toString());
if (mErNo6.getText().length() == 1) {
mErNo7.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo6 = mErNo6.getText().toString();


mErNo7 = (EditText) v.findViewById(R.id.erno_7);
mErNo7.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo7(s.toString());
if (mErNo7.getText().length() == 1) {
mErNo8.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo7 = mErNo7.getText().toString();


mErNo8 = (EditText) v.findViewById(R.id.erno_8);
mErNo8.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo8(s.toString());
if (mErNo8.getText().length() == 1) {
mErNo9.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo8 = mErNo8.getText().toString();

mErNo9 = (EditText) v.findViewById(R.id.erno_9);
mErNo9.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo9(s.toString());
if (mErNo9.getText().length() == 1) {
mErNo10.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo9 = mErNo9.getText().toString();


mErNo10 = (EditText) v.findViewById(R.id.erno_10);
mErNo10.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo10(s.toString());
if (mErNo10.getText().length() == 1) {
mErNo11.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo10 = mErNo10.getText().toString();


mErNo11 = (EditText) v.findViewById(R.id.erno_11);
mErNo11.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo11(s.toString());
if (mErNo11.getText().length() == 1) {
mErNo12.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo11 = mErNo11.getText().toString();


mErNo12 = (EditText) v.findViewById(R.id.erno_12);
mErNo12.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo12(s.toString());
if (mErNo12.getText().length() == 1) {
mUserName.requestFocus();
}


}

@Override
public void afterTextChanged (Editable s) {

}
});
erNo12 = mErNo12.getText().toString();


//USER_NAME editText field in fragment_user_sign_up
mUserName = (EditText) v.findViewById(R.id.userName_editText);
mUserName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setUserName(s.toString());

}

@Override
public void afterTextChanged (Editable s) {

}
});
final String userName = mUserName.getText().toString();


//FULL_NAME editText field in fragment_user_sign_up
mFullName = (EditText) v.findViewById(R.id.fullName_editText);
mFullName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setFullName(s.toString());

}

@Override
public void afterTextChanged (Editable s) {

}
});
final String fullName = mFullName.getText().toString();


//PASSWORD editText field in fragment_user_sign_up
mPassword = (EditText) v.findViewById(R.id.passwd_editText);
mPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setPassword(s.toString());

}

@Override
public void afterTextChanged (Editable s) {

}
});
final String pass = mPassword.getText().toString();


//confirmPassword editText field in fragment_user_sign_up
mConfirmPassword = (EditText) v.findViewById(R.id.confirm_passwd_editText);
mConfirmPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setCorrectPassword(s.toString());

}

@Override
public void afterTextChanged (Editable s) {

}
});
final String cPass = mConfirmPassword.getText().toString();


//EMAIL Id of user
mEmail = (EditText)v.findViewById(R.id.emai_id_editText);
mEmail.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setEmail(s.toString());

}

@Override
public void afterTextChanged (Editable s) {

}
});


//datOFBirth button
mDob = (Button) v.findViewById(R.id.dob_button);
updateDate();
mDob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
FragmentManager manager = getFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mUserData.getDob());
dialog.setTargetFragment(UserSignUpFragment.this, REQUEST_DATE);
dialog.show(manager, DIALOG_DATE);
}
});


//submit button
mSubmit = (Button) v.findViewById(R.id.submit_button);
mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {

if (mUserName.length() == 0) {
createToast("Enter UserName");
} else if (mFullName.length() == 0) {
createToast("Enter FullName");
} else if (mPassword.length() == 0) {
createToast("Enter Password");
} else if (!mUserData.getPassword().equals(mUserData.getCorrectPassword())) {
createToast("Password And Confirm PassWord does not match");
} else if (mErNo1.length() == 0 &&
mErNo2.length() == 0 &&
mErNo3.length() == 0 &&
mErNo4.length() == 0 &&
mErNo5.length() == 0 &&
mErNo6.length() == 0 &&
mErNo7.length() == 0 &&
mErNo8.length() == 0 &&
mErNo9.length() == 0 &&
mErNo10.length() == 0 &&
mErNo11.length() == 0 &&
mErNo12.length() == 0 ) {
createToast("Enter Enrollment Number");
}
else {
//Intent i = UserProfileActivity.newIntent(getActivity(), mUserData.getUserName(), mUserData.getFullName(), fetchEnrollmentNumber());
// startActivity(i);


try{
Message.message("Database Created",getActivity());
createNewUser();
}catch (SQLException e){
Toast.makeText(getActivity(), "DATABASE NOT SAVED", Toast.LENGTH_SHORT).show();
}

}


}
});


return v;
}

public void createToast (String toast) {
Toast.makeText(getActivity(), toast, Toast.LENGTH_SHORT).show();
}

public String fetchEnrollmentNumber () {
String ErNo = mUserData.getErNo1() +
mUserData.getErNo2() +
mUserData.getErNo3() +
mUserData.getErNo4() +
mUserData.getErNo5() +
mUserData.getErNo6() +
mUserData.getErNo7() +
mUserData.getErNo8() +
mUserData.getErNo9() +
mUserData.getErNo10() +
mUserData.getErNo11() +
mUserData.getErNo12();

mUserData.setEnrollmentNumber(ErNo);

return mUserData.getEnrollmentNumber();
}
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}

if (requestCode == REQUEST_DATE ) {
Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
mUserData.setDob(date);
updateDate();
}
}

private void updateDate () {
mDob.setText(formatDate.format(mUserData.getDob()));

}

public void createNewUser(){


mUserData.getUserName();
mUserData.getPassword();
UserLab.get(getActivity()).addUser(mUserData);
}


}

So I would appreciate some help or some other way around to make this work.

Answer

You've gotten the text from the EditText immediately when the view is created.

//user UserName editText in fragment_user_login
mUserName = (EditText) findViewById(R.id.login_user_name);
userName = mUserName.getText().toString();

//PASSWORD editText
mUserPassword = (EditText) findViewById(R.id.login_user_pass);
pass = mUserPassword.getText().toString();

Both the strings are empty.

You should instead only find the views, then when you click on the button, get the texts.

Also move this into the button click

password = mHelper.fetchUser(userName);

Also, storing plaintext passwords is rule #1 not to do in password management, but that's outside the scope of the question