RegistrationActivity.java
package com.Registration;
import com.AppDemo.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
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 RegistrationActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
DBAdapter db;
// Declare variables
EditText fname;
EditText lname;
EditText dob;
EditText address;
EditText mobile;
EditText email;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
// Get the resource
fname = (EditText) findViewById(R.id.edt_Fname);
lname = (EditText) findViewById(R.id.edt_Lnamme);
dob = (EditText) findViewById(R.id.edt_Dob);
address = (EditText) findViewById(R.id.edt_Address);
mobile = (EditText) findViewById(R.id.edt_Mobile);
email = (EditText) findViewById(R.id.edt_Email);
Button submit = (Button) findViewById(R.id.btn_add);
Button contact = (Button) findViewById(R.id.btn_addcontact);
submit.setOnClickListener(this);
contact.setOnClickListener(this);
db = new DBAdapter(this);
getData();
}
private void addData(String fname, String lname, String dob,
String address, String mobile, String email) {
// TODO Auto-generated method stub
db.open();
if (db.insertContact(fname, lname, dob, address, mobile, email) >= 0) {
Toast.makeText(this, "Add successful.", Toast.LENGTH_LONG).show();
}
db.close();
}
private void getData() {
// TODO Auto-generated method stub
db.open();
Cursor c = db.getAllContacts();
if (c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
}
public void DisplayContact(Cursor c) {
Toast.makeText(
this,
"Id: " + c.getString(0) + "\n" + "FName: " + c.getString(1)
+ "\n" + "LName: " + c.getString(2) + "\n" + "DOB: "
+ c.getString(3) + "\n" + "Address: " + c.getString(4)
+ "\n" + "Mobile :" + c.getString(5) + "\n" + "Email: "
+ c.getString(6), Toast.LENGTH_LONG).show();
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_add:
String name = fname.getText().toString();
String surname = lname.getText().toString();
String dobirth = dob.getText().toString();
String addres = address.getText().toString();
String mobileno = mobile.getText().toString();
String email_id = email.getText().toString();
Toast.makeText(
RegistrationActivity.this,
"Name: " + name + "\n" + "Lname: " + surname + "\n" + "DOB: "
+ dobirth + "\n" + "Address: " + addres + "\n"
+ "Mob: " + mobileno + "\n" + "Email: " + email_id
+ "\n", Toast.LENGTH_LONG).show();
addData(name, surname, dobirth, addres, mobileno, email_id);
name = "";
surname = "";
dobirth = "";
addres = "";
mobileno = "";
email_id = "";
break;
case R.id.btn_addcontact:
startActivity(new Intent(RegistrationActivity.this, Calculation.class));
break;
default:
break;
}
}
}
DBAdapter.java
package com.Registration;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "id";
static final String KEY_FNAME = "fname";
static final String KEY_LNAME = "lname";
static final String KEY_DATE_OF_BIRTH = "dob";
static final String KEY_CREATED_ON_DATE = "created_on";
static final String KEY_ADDRESS = "address";
static final String KEY_MOBILE = "mobile";
static final String KEY_EMAIL = "email";
static final String KEY_CAL_ID = "cal_id";
static final String KEY_CAL_TITAL = "cal_tital";
static final String KEY_CAL_DESCRIPTION = "ccal_description";
static final String KEY_CAL_CREATED_DATE = "cal_created_date";
static final String KEY_CAL_EXP_DATE = "cal_expiry_date";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "Personal";
static final String DATABASE_REGISTRATION_TABLE = "Registration";
static final String DATABASE_CALCULATION_TABLE = "Calculation";
static final int DATABASE_VERSION = 1;
/*public void onCreate(SQLiteDatabase db) {
String CREATE_REGISTRATION_TABLE = "CREATE TABLE " + DATABASE_REGISTRATION_TABLE + "("
+ KEY_ROWID + " INTEGER PRIMARY KEY," + KEY_FNAME + " TEXT," + KEY_LNAME + "TEXT,"+ KEY_ADDRESS + "TEXT,"
+ KEY_MOBILE + " TEXT," + KEY_EMAIL + " TEXT" +")";
db.execSQL(DATABASE_REGISTRATION_TABLE);
}*/
static final String DATABASE_CREATE = "create table Registration (id integer primary key autoincrement, "
+ "fname text not null, lname text not null, dob text null, created_on text null, address text not null, mobile text not null, email text not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
Calendar currentDate;
SimpleDateFormat formatter;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Registration");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
}
// ---insert a contact into the database---
public long insertContact(String fname, String lname, String dob, String address, String mobile, String email) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_FNAME, fname);
initialValues.put(KEY_LNAME, lname);
initialValues.put(KEY_DATE_OF_BIRTH, dob);
//initialValues.put(KEY_CREATED_ON_DATE, formatter.format(currentDate.getTime()));
initialValues.put(KEY_ADDRESS, address);
initialValues.put(KEY_MOBILE, mobile);
initialValues.put(KEY_EMAIL, email);
return db.insert(DATABASE_REGISTRATION_TABLE, null, initialValues);
}
// ---deletes a particular contact---
public boolean deleteContact(long rowId) {
return db.delete(DATABASE_REGISTRATION_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
// ---retrieves all the contacts---
public Cursor getAllContacts() {
return db.query(DATABASE_REGISTRATION_TABLE, new String[] { KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_DATE_OF_BIRTH, KEY_ADDRESS, KEY_MOBILE,
KEY_EMAIL }, null, null, null, null, null);
}
// ---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_REGISTRATION_TABLE, new String[] {
KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_DATE_OF_BIRTH, KEY_ADDRESS, KEY_MOBILE, KEY_EMAIL }, KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
// ---updates a contact---
public boolean updateContact(long rowId, String fname, String lname, String dob, String address, String mobile, String email) {
ContentValues args = new ContentValues();
args.put(KEY_FNAME, fname);
args.put(KEY_LNAME, lname);
args.put(KEY_DATE_OF_BIRTH, dob);
args.put(KEY_ADDRESS, address);
args.put(KEY_MOBILE, mobile);
args.put(KEY_EMAIL, email);
return db.update(DATABASE_REGISTRATION_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
package com.Registration;
import com.AppDemo.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
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 RegistrationActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
DBAdapter db;
// Declare variables
EditText fname;
EditText lname;
EditText dob;
EditText address;
EditText mobile;
EditText email;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
// Get the resource
fname = (EditText) findViewById(R.id.edt_Fname);
lname = (EditText) findViewById(R.id.edt_Lnamme);
dob = (EditText) findViewById(R.id.edt_Dob);
address = (EditText) findViewById(R.id.edt_Address);
mobile = (EditText) findViewById(R.id.edt_Mobile);
email = (EditText) findViewById(R.id.edt_Email);
Button submit = (Button) findViewById(R.id.btn_add);
Button contact = (Button) findViewById(R.id.btn_addcontact);
submit.setOnClickListener(this);
contact.setOnClickListener(this);
db = new DBAdapter(this);
getData();
}
private void addData(String fname, String lname, String dob,
String address, String mobile, String email) {
// TODO Auto-generated method stub
db.open();
if (db.insertContact(fname, lname, dob, address, mobile, email) >= 0) {
Toast.makeText(this, "Add successful.", Toast.LENGTH_LONG).show();
}
db.close();
}
private void getData() {
// TODO Auto-generated method stub
db.open();
Cursor c = db.getAllContacts();
if (c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
}
public void DisplayContact(Cursor c) {
Toast.makeText(
this,
"Id: " + c.getString(0) + "\n" + "FName: " + c.getString(1)
+ "\n" + "LName: " + c.getString(2) + "\n" + "DOB: "
+ c.getString(3) + "\n" + "Address: " + c.getString(4)
+ "\n" + "Mobile :" + c.getString(5) + "\n" + "Email: "
+ c.getString(6), Toast.LENGTH_LONG).show();
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_add:
String name = fname.getText().toString();
String surname = lname.getText().toString();
String dobirth = dob.getText().toString();
String addres = address.getText().toString();
String mobileno = mobile.getText().toString();
String email_id = email.getText().toString();
Toast.makeText(
RegistrationActivity.this,
"Name: " + name + "\n" + "Lname: " + surname + "\n" + "DOB: "
+ dobirth + "\n" + "Address: " + addres + "\n"
+ "Mob: " + mobileno + "\n" + "Email: " + email_id
+ "\n", Toast.LENGTH_LONG).show();
addData(name, surname, dobirth, addres, mobileno, email_id);
name = "";
surname = "";
dobirth = "";
addres = "";
mobileno = "";
email_id = "";
break;
case R.id.btn_addcontact:
startActivity(new Intent(RegistrationActivity.this, Calculation.class));
break;
default:
break;
}
}
}
DBAdapter.java
package com.Registration;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "id";
static final String KEY_FNAME = "fname";
static final String KEY_LNAME = "lname";
static final String KEY_DATE_OF_BIRTH = "dob";
static final String KEY_CREATED_ON_DATE = "created_on";
static final String KEY_ADDRESS = "address";
static final String KEY_MOBILE = "mobile";
static final String KEY_EMAIL = "email";
static final String KEY_CAL_ID = "cal_id";
static final String KEY_CAL_TITAL = "cal_tital";
static final String KEY_CAL_DESCRIPTION = "ccal_description";
static final String KEY_CAL_CREATED_DATE = "cal_created_date";
static final String KEY_CAL_EXP_DATE = "cal_expiry_date";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "Personal";
static final String DATABASE_REGISTRATION_TABLE = "Registration";
static final String DATABASE_CALCULATION_TABLE = "Calculation";
static final int DATABASE_VERSION = 1;
/*public void onCreate(SQLiteDatabase db) {
String CREATE_REGISTRATION_TABLE = "CREATE TABLE " + DATABASE_REGISTRATION_TABLE + "("
+ KEY_ROWID + " INTEGER PRIMARY KEY," + KEY_FNAME + " TEXT," + KEY_LNAME + "TEXT,"+ KEY_ADDRESS + "TEXT,"
+ KEY_MOBILE + " TEXT," + KEY_EMAIL + " TEXT" +")";
db.execSQL(DATABASE_REGISTRATION_TABLE);
}*/
static final String DATABASE_CREATE = "create table Registration (id integer primary key autoincrement, "
+ "fname text not null, lname text not null, dob text null, created_on text null, address text not null, mobile text not null, email text not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
Calendar currentDate;
SimpleDateFormat formatter;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Registration");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
}
// ---insert a contact into the database---
public long insertContact(String fname, String lname, String dob, String address, String mobile, String email) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_FNAME, fname);
initialValues.put(KEY_LNAME, lname);
initialValues.put(KEY_DATE_OF_BIRTH, dob);
//initialValues.put(KEY_CREATED_ON_DATE, formatter.format(currentDate.getTime()));
initialValues.put(KEY_ADDRESS, address);
initialValues.put(KEY_MOBILE, mobile);
initialValues.put(KEY_EMAIL, email);
return db.insert(DATABASE_REGISTRATION_TABLE, null, initialValues);
}
// ---deletes a particular contact---
public boolean deleteContact(long rowId) {
return db.delete(DATABASE_REGISTRATION_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
// ---retrieves all the contacts---
public Cursor getAllContacts() {
return db.query(DATABASE_REGISTRATION_TABLE, new String[] { KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_DATE_OF_BIRTH, KEY_ADDRESS, KEY_MOBILE,
KEY_EMAIL }, null, null, null, null, null);
}
// ---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_REGISTRATION_TABLE, new String[] {
KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_DATE_OF_BIRTH, KEY_ADDRESS, KEY_MOBILE, KEY_EMAIL }, KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
// ---updates a contact---
public boolean updateContact(long rowId, String fname, String lname, String dob, String address, String mobile, String email) {
ContentValues args = new ContentValues();
args.put(KEY_FNAME, fname);
args.put(KEY_LNAME, lname);
args.put(KEY_DATE_OF_BIRTH, dob);
args.put(KEY_ADDRESS, address);
args.put(KEY_MOBILE, mobile);
args.put(KEY_EMAIL, email);
return db.update(DATABASE_REGISTRATION_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
No comments:
Post a Comment