我创建了一个登录页面和一个注册页面。注册到应用程序后,我无法使用这些值进行登录;无论我在注册期间输入的内容如何,我都无法使用凭据登录。请帮助
我的代码是
DBHelper.java
public class DBHelper extends SQLiteOpenHelper
{
private SQLiteDatabase db;
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "firstname";
public static final String KEY_LNAME = "lastname";
public static final String KEY_GENDER = "gender";
public static final String KEY_USER = "username";
public static final String KEY_EMAIL = "email";
DBHelper DB = null;
private static final String DATABASE_NAME = "srikanth1.db";
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_TABLE_NAME = "sri1";
private static final String DATABASE_TABLE_CREATE =
"CREATE TABLE " + DATABASE_TABLE_NAME + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(DATABASE_TABLE_CREATE);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}
public void open() {
getWritableDatabase();
}
public Cursor getDetails(String text) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE_NAME,
new String[]{KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER, KEY_USER, KEY_EMAIL},
KEY_USER + "=" + text,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
}
Registration.java
public class Registration extends Activity implements OnClickListener, OnItemSelectedListener
{
// Variable Declaration should be in onCreate()
private Button mSubmit;
private Button mCancel;
private EditText mFname;
private EditText mLname;
private EditText mUsername;
private EditText mPassword;
private EditText mEmail;
private Spinner mGender;
private String Gen;
protected DBHelper DB = new DBHelper(Registration.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
//Assignment of UI fields to the variables
mSubmit = (Button)findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
mCancel = (Button)findViewById(R.id.cancel);
mCancel.setOnClickListener(this);
mFname = (EditText)findViewById(R.id.efname);
mLname = (EditText)findViewById(R.id.elname);
mUsername = (EditText)findViewById(R.id.reuname);
mPassword = (EditText)findViewById(R.id.repass);
mEmail = (EditText)findViewById(R.id.eemail);
mGender = (Spinner)findViewById(R.id.spinner1);
// Spinner method to read the on selected value
ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
android.R.layout.simple_spinner_item, new State[] {
new State("Male"),
new State("Female")});
mGender.setAdapter(spinnerArrayAdapter);
mGender.setOnItemSelectedListener(this);
}
public void onClick(View v)
{
/* int id = v.getId();
if (id == R.id.cancel) {
Intent i = new Intent(getBaseContext(), LoginActivity.class);
startActivity(i);
} else if (id == R.id.submit) {
String fname = mFname.getText().toString();
String lname = mLname.getText().toString();
String uname = mUsername.getText().toString();
String pass = mPassword.getText().toString();
String email = mEmail.getText().toString();
boolean invalid = false;
if(fname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your Firstname", Toast.LENGTH_SHORT).show();
}
else
if(lname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Lastname", Toast.LENGTH_SHORT).show();
}
else
if(uname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Username", Toast.LENGTH_SHORT).show();
}
else
if(pass.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
}
else
if(email.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Email ID", Toast.LENGTH_SHORT).show();
}
else
if(invalid == false)
{
addEntry(fname, lname, Gen, uname, pass, email);
Intent i_register = new Intent(Registration.this, LoginActivity.class);
startActivity(i_register);
//finish();
}
}*/
switch(v.getId()){
case R.id.cancel:
Intent i = new Intent(getBaseContext(), LoginActivity.class);
startActivity(i);
//finish();
break;
case R.id.submit:
String fname = mFname.getText().toString();
String lname = mLname.getText().toString();
String uname = mUsername.getText().toString();
String pass = mPassword.getText().toString();
String email = mEmail.getText().toString();
boolean invalid = false;
if(fname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your Firstname", Toast.LENGTH_SHORT).show();
}
else
if(lname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Lastname", Toast.LENGTH_SHORT).show();
}
else
if(uname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Username", Toast.LENGTH_SHORT).show();
}
else
if(pass.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
}
else
if(email.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Email ID", Toast.LENGTH_SHORT).show();
}
else
if(invalid == false)
{
addEntry(fname, lname, Gen, uname, pass, email);
Intent i_register = new Intent(Registration.this, LoginActivity.class);
startActivity(i_register);
//finish();
}
break;
}
}
public void onDestroy()
{
super.onDestroy();
DB.close();
}
private void addEntry(String fname, String lname, String Gen, String uname, String pass, String email)
{
SQLiteDatabase db = DB.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("firstname", fname);
values.put("lastname", lname);
values.put("gender", Gen);
values.put("username", uname);
values.put("password", pass);
values.put("email", email);
try
{
db.insert(DBHelper.DATABASE_TABLE_NAME, null, values);
Toast.makeText(getApplicationContext(), "your details submitted Successfully...", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// Get the currently selected State object from the spinner
State st = (State)mGender.getSelectedItem();
// Show it via a toast
toastState( "onItemSelected", st );
}
public void toastState(String name, State st)
{
if ( st != null )
{
Gen = st.name;
//Toast.makeText(getBaseContext(), Gen, Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
LoginActivity .java
public class LoginActivity extends Activity implements OnClickListener
{
Button mLogin;
Button mRegister;
EditText muname;
EditText mpassword;
DBHelper DB = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mRegister = (Button)findViewById(R.id.register);
mRegister.setOnClickListener(this);
mLogin = (Button)findViewById(R.id.login);
mLogin.setOnClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.register:
Intent i = new Intent(getBaseContext(), Registration.class);
startActivity(i);
break;
case R.id.login:
muname = (EditText)findViewById(R.id.Ledituname);
mpassword = (EditText)findViewById(R.id.Leditpw);
String username = muname.getText().toString();
String password = mpassword.getText().toString();
if(username.equals("") || username == null)
{
Toast.makeText(getApplicationContext(), "Please enter User Name", Toast.LENGTH_SHORT).show();
}
else if(password.equals("") || password == null)
{
Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
}
else
{
boolean validLogin = validateLogin(username, password, getBaseContext());
if(validLogin)
{
//System.out.println("In Valid");
Intent in = new Intent(getBaseContext(), TabBar.class);
//in.putExtra("UserName", muname.getText().toString());
startActivity(in);
//finish();
}
}
break;
}
}
private boolean validateLogin(String username, String password, Context baseContext)
{
DB = new DBHelper(getBaseContext());
SQLiteDatabase db = DB.getReadableDatabase();
String[] columns = {"_id"};
String selection = "username=? AND password=?";
String[] selectionArgs = {username,password};
Cursor cursor = null;
try{
cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection, selectionArgs, null, null, null);
startManagingCursor(cursor);
}
catch(Exception e)
{
e.printStackTrace();
}
int numberOfRows = cursor.getCount();
if(numberOfRows <= 0)
{
Toast.makeText(getApplicationContext(), "User Name and Password miss match..\nPlease Try Again", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
startActivity(intent);
return false;
}
return true;
}
public void onDestroy()
{
super.onDestroy();
DB.close();
}
}
register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/efname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true">
<requestFocus />
</EditText>
<TextView
android:id="@+id/lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/elname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"/>
<TextView
android:id="@+id/rgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:drawSelectorOnTop="true" />
<TextView
android:id="@+id/runame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/reuname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/rpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/repass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toRightOf="@id/submit"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Luname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/Ledituname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<requestFocus />
</EditText>
<TextView
android:id="@+id/Lpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/Leditpw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Login" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_toRightOf="@id/login"/>
</RelativeLayout>
LogCatFile
06-26 07:07:17.980: E/AndroidRuntime(791): FATAL EXCEPTION: main
06-26 07:07:17.980: E/AndroidRuntime(791): Process: com.example.app, PID: 791
06-26 07:07:17.980: E/AndroidRuntime(791): java.lang.NullPointerException
06-26 07:07:17.980: E/AndroidRuntime(791): at com.example.app.LoginActivity.validateLogin(LoginActivity.java:109)
06-26 07:07:17.980: E/AndroidRuntime(791): at com.example.app.LoginActivity.onClick(LoginActivity.java:71)
06-26 07:07:17.980: E/AndroidRuntime(791): at android.view.View.performClick(View.java:4438)
06-26 07:07:17.980: E/AndroidRuntime(791): at android.view.View$PerformClick.run(View.java:18422)
06-26 07:07:17.980: E/AndroidRuntime(791): at android.os.Handler.handleCallback(Handler.java:733)
06-26 07:07:17.980: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:95)
06-26 07:07:17.980: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:136)
06-26 07:07:17.980: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-26 07:07:17.980: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 07:07:17.980: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:515)
06-26 07:07:17.980: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-26 07:07:17.980: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-26 07:07:17.980: E/AndroidRuntime(791): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
替换它:
DB = new DBHelper(getBaseContext());
到此,
DB = new DBHelper(LoginActivity.this);
答案 1 :(得分:0)
我检查了代码并且工作正常。附加我用于屏幕截图的代码。不确定您是否使用了正确的导入,我已将代码附加到导入。
代码如下,
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Luname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/Ledituname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<requestFocus />
</EditText>
<TextView
android:id="@+id/Lpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/Leditpw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Login" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_toRightOf="@id/login"/>
</RelativeLayout>
</LinearLayout>
register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/efname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true">
<requestFocus />
</EditText>
<TextView
android:id="@+id/lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/elname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"/>
<TextView
android:id="@+id/rgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:drawSelectorOnTop="true" />
<TextView
android:id="@+id/runame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/reuname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/rpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/repass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toRightOf="@id/submit"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
DBHelper.java
package com.example.test;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private SQLiteDatabase db;
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "firstname";
public static final String KEY_LNAME = "lastname";
public static final String KEY_GENDER = "gender";
public static final String KEY_USER = "username";
public static final String KEY_EMAIL = "email";
DBHelper DB = null;
private static final String DATABASE_NAME = "srikanth1.db";
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_TABLE_NAME = "sri1";
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_TABLE_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}
public void open() {
getWritableDatabase();
}
public Cursor getDetails(String text) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE_NAME, new String[] {
KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER, KEY_USER,
KEY_EMAIL }, KEY_USER + "=" + text, null, null, null, null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
Registration.java
package com.example.test;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Registration extends Activity implements OnClickListener,
OnItemSelectedListener {
// Variable Declaration should be in onCreate()
private Button mSubmit;
private Button mCancel;
private EditText mFname;
private EditText mLname;
private EditText mUsername;
private EditText mPassword;
private EditText mEmail;
private Spinner mGender;
private String Gen;
protected DBHelper DB = new DBHelper(Registration.this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
// Assignment of UI fields to the variables
mSubmit = (Button) findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
mCancel = (Button) findViewById(R.id.cancel);
mCancel.setOnClickListener(this);
mFname = (EditText) findViewById(R.id.efname);
mLname = (EditText) findViewById(R.id.elname);
mUsername = (EditText) findViewById(R.id.reuname);
mPassword = (EditText) findViewById(R.id.repass);
mEmail = (EditText) findViewById(R.id.eemail);
mGender = (Spinner) findViewById(R.id.spinner1);
// Spinner method to read the on selected value
ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
android.R.layout.simple_spinner_item, new State[] {
new State("Male"), new State("Female") });
mGender.setAdapter(spinnerArrayAdapter);
mGender.setOnItemSelectedListener(this);
}
@Override
public void onClick(View v) {
/*
* int id = v.getId(); if (id == R.id.cancel) { Intent i = new
* Intent(getBaseContext(), LoginActivity.class); startActivity(i); }
* else if (id == R.id.submit) { String fname =
* mFname.getText().toString(); String lname =
* mLname.getText().toString();
*
* String uname = mUsername.getText().toString(); String pass =
* mPassword.getText().toString(); String email =
* mEmail.getText().toString();
*
*
* boolean invalid = false;
*
* if(fname.equals("")) { invalid = true;
* Toast.makeText(getApplicationContext(), "Enter your Firstname",
* Toast.LENGTH_SHORT).show(); } else
*
* if(lname.equals("")) { invalid = true;
* Toast.makeText(getApplicationContext(), "Please enter your Lastname",
* Toast.LENGTH_SHORT).show(); } else
*
* if(uname.equals("")) { invalid = true;
* Toast.makeText(getApplicationContext(), "Please enter your Username",
* Toast.LENGTH_SHORT).show(); } else
*
*
* if(pass.equals("")) { invalid = true;
* Toast.makeText(getApplicationContext(), "Please enter your Password",
* Toast.LENGTH_SHORT).show();
*
* } else if(email.equals("")) { invalid = true;
* Toast.makeText(getApplicationContext(), "Please enter your Email ID",
* Toast.LENGTH_SHORT).show(); } else if(invalid == false) {
* addEntry(fname, lname, Gen, uname, pass, email); Intent i_register =
* new Intent(Registration.this, LoginActivity.class);
* startActivity(i_register); //finish(); } }
*/
switch (v.getId()) {
case R.id.cancel:
Intent i = new Intent(getBaseContext(), LoginActivity.class);
startActivity(i);
// finish();
break;
case R.id.submit:
String fname = mFname.getText().toString();
String lname = mLname.getText().toString();
String uname = mUsername.getText().toString();
String pass = mPassword.getText().toString();
String email = mEmail.getText().toString();
boolean invalid = false;
if (fname.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your Firstname",
Toast.LENGTH_SHORT).show();
} else
if (lname.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your Lastname", Toast.LENGTH_SHORT)
.show();
} else
if (uname.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your Username", Toast.LENGTH_SHORT)
.show();
} else
if (pass.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your Password", Toast.LENGTH_SHORT)
.show();
} else if (email.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your Email ID", Toast.LENGTH_SHORT)
.show();
} else if (invalid == false) {
addEntry(fname, lname, Gen, uname, pass, email);
Intent i_register = new Intent(Registration.this,
LoginActivity.class);
startActivity(i_register);
// finish();
}
break;
}
}
public void onDestroy() {
super.onDestroy();
DB.close();
}
private void addEntry(String fname, String lname, String Gen, String uname,
String pass, String email) {
SQLiteDatabase db = DB.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("firstname", fname);
values.put("lastname", lname);
values.put("gender", Gen);
values.put("username", uname);
values.put("password", pass);
values.put("email", email);
try {
db.insert(DBHelper.DATABASE_TABLE_NAME, null, values);
Toast.makeText(getApplicationContext(),
"your details submitted Successfully...",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// Get the currently selected State object from the spinner
State st = (State) mGender.getSelectedItem();
// Show it via a toast
toastState("onItemSelected", st);
}
public void toastState(String name, State st) {
if (st != null) {
Gen = st.name;
// Toast.makeText(getBaseContext(), Gen, Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
LoginActivity.java
package com.example.test;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity implements OnClickListener {
Button mLogin;
Button mRegister;
EditText muname;
EditText mpassword;
DBHelper DB = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mRegister = (Button) findViewById(R.id.register);
mRegister.setOnClickListener(this);
mLogin = (Button) findViewById(R.id.login);
mLogin.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
Intent i = new Intent(getBaseContext(), Registration.class);
startActivity(i);
break;
case R.id.login:
muname = (EditText) findViewById(R.id.Ledituname);
mpassword = (EditText) findViewById(R.id.Leditpw);
String username = muname.getText().toString();
String password = mpassword.getText().toString();
if (username.equals("") || username == null) {
Toast.makeText(getApplicationContext(),
"Please enter User Name", Toast.LENGTH_SHORT).show();
} else if (password.equals("") || password == null) {
Toast.makeText(getApplicationContext(),
"Please enter your Password", Toast.LENGTH_SHORT)
.show();
} else {
boolean validLogin = validateLogin(username, password,
getBaseContext());
if (validLogin) {
// System.out.println("In Valid");
//Intent in = new Intent(getBaseContext(), TabBar.class);
// in.putExtra("UserName", muname.getText().toString());
//startActivity(in);
Toast.makeText(getApplicationContext(),
"Success Valid Login", Toast.LENGTH_SHORT)
.show();
// finish();
}
}
break;
}
}
private boolean validateLogin(String username, String password,
Context baseContext) {
DB = new DBHelper(getBaseContext());
SQLiteDatabase db = DB.getReadableDatabase();
String[] columns = { "_id" };
String selection = "username=? AND password=?";
String[] selectionArgs = { username, password };
Cursor cursor = null;
try {
cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
startManagingCursor(cursor);
} catch (Exception e)
{
e.printStackTrace();
}
int numberOfRows = cursor.getCount();
if (numberOfRows <= 0) {
Toast.makeText(getApplicationContext(),
"User Name and Password miss match..\nPlease Try Again",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
startActivity(intent);
return false;
}
return true;
}
public void onDestroy() {
super.onDestroy();
DB.close();
}
}
State.java
package com.example.test;
public class State {
public String name;
public State(String name)
{
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString()
{
return name;
}
}