DBAdapter.java:-
package com.my.members;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.Cursor;
public class DBAdapter
import android.database.sqlite.SQLiteDatabase;
{
public static final String KEY_ROW_ID = "_id";
private static final String DATABASE_TABLE = "members";
public static final String KEY_PASSWORD = "password";
public static final String KEY_USERNAME = "username";
SQLiteDatabase mDb;
Context mCtx;
public DBAdapter open() throws SQLException
DBHelper mDbHelper;
public DBAdapter(Context context)
{
this.mCtx = context;
}
{
{
mDbHelper = new DBHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close()
ContentValues initialValues = new ContentValues();
mDbHelper.close();
}
public long register(String user,String pw)
{
initialValues.put(KEY_USERNAME, user);
public boolean Login(String username, String password) throws SQLException
initialValues.put(KEY_PASSWORD, pw);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
{
if (mCursor != null) {
Cursor mCursor = mDb.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
if(mCursor.getCount() > 0)
{
return true;
}
}
}
return false;
}
DBHelper.java:-
package com.my.members;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
private static final String DATABASE_NAME = "membersdb";
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE TABLE (_id integer primary key autoincrement,username text not null,password text not null);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
}
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS members");
onCreate(db);
}
MemberActivity.java:-
package com.my.members;
import android.app.Activity;
import android.content.Context;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.Button;
import android.widget.EditText;
EditText txtUserName;
public class MembersActivity extends Activity {
DBAdapter dbAdapter;
EditText txtPassword;
Button btnLogin;
super.onCreate(savedInstanceState);
Button btnRegister;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
btnLogin = (Button) findViewById(R.id.btn_login);
txtUserName = (EditText) findViewById(R.id.et_user);
txtPassword = (EditText) findViewById(R.id.et_pw);
btnRegister = (Button) findViewById(R.id.btn_reg);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
dbAdapter = new DBAdapter(this);
dbAdapter.open();
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String password = txtPassword.getText().toString();
imm.hideSoftInputFromWindow(txtUserName.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
String username = txtUserName.getText().toString();
.show();
if (username.length() > 0 && password.length() > 0) {
try {
if (dbAdapter.Login(username, password)) {
Toast.makeText(MembersActivity.this,
"Successfully Logged In", Toast.LENGTH_LONG)
} else {
Toast.LENGTH_LONG).show();
Toast.makeText(MembersActivity.this,
"Invalid username or password",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(MembersActivity.this, "Some problem occurred",
}
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
Toast.makeText(MembersActivity.this,
"Username or Password is empty", Toast.LENGTH_LONG).show();
}
}
});
btnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
long i = dbAdapter.register(username, password);
imm.hideSoftInputFromWindow(txtUserName.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
try {
String username = txtUserName.getText().toString();
String password = txtPassword.getText().toString();
if(i != -1)
}
Toast.makeText(MembersActivity.this, "You have successfully registered",Toast.LENGTH_LONG).show();
} catch (SQLException e) {
Toast.makeText(MembersActivity.this, "Some problem occurred",
Toast.LENGTH_LONG).show();
}
}
});
}
main.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" android:textSize="40dp" android:layout_gravity="right" android:layout_marginBottom="10dp" android:layout_marginLeft="30dp"/>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/et_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:width="250dp" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:hint="@string/u">
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/et_pw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:hint="@string/p"/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/l" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:textSize="20dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:textSize="20dp"/>
</TableRow>
</TableLayout>
</LinearLayout>
No comments:
Post a Comment