Store Register Layout data to SqLite DataBase :-
STEP:-1 Create New Android Project.
Ex:-RegSqlliteDataBase
STEP:-2 Go to res/layout open activity_main.xml file and Edit it.
:-copy below code and paste it you will get the reg layout.
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
< EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="26dp"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName" >
< / EditText>
< EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName" />
< EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Mobile Number"
android:inputType="textPersonName" />
< EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginTop="27dp"
android:ems="10"
android:hint="Email id"
android:inputType="textPersonName" />
< Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText4"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:text="SUBMIT" />
< Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText4"
android:layout_marginRight="14dp"
android:text="Login" />
< / RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
< EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="26dp"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName" >
< / EditText>
< EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName" />
< EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Mobile Number"
android:inputType="textPersonName" />
< EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginTop="27dp"
android:ems="10"
android:hint="Email id"
android:inputType="textPersonName" />
< Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText4"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:text="SUBMIT" />
< Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText4"
android:layout_marginRight="14dp"
android:text="Login" />
< / RelativeLayout>
STEP:- 2 Create the One class in src/example/regsqlitedatabase for create the database and create the table.It should be extends from the SQLiteOpenHelper.
:-see the following code
:- Open MyDbHelper.java file put the following code.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDbHelper extends SQLiteOpenHelper{
public static final int VERSION = 2;
public static final String DBNAME = "Register.db";
public static final String TABLENAME="Reg";
public static final String NAME = "NAME";
public static final String MNAME = "LNAME";
public static final String EMAIL = "EMAIL";
public static final String MNO = "MNO";
public MyDbHelper(Context context) {
//context is use for the create the database or open the database
//DBNAME is the name of the database
//null:- CursorFactory(Interface) its creating the cursor object .
//default value of object is null.
// version of the database.
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLENAME+"("+NAME+" text,"+MNAME+" text, "+MNO+" text,"+EMAIL+" text)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDbHelper extends SQLiteOpenHelper{
public static final int VERSION = 2;
public static final String DBNAME = "Register.db";
public static final String TABLENAME="Reg";
public static final String NAME = "NAME";
public static final String MNAME = "LNAME";
public static final String EMAIL = "EMAIL";
public static final String MNO = "MNO";
public MyDbHelper(Context context) {
//context is use for the create the database or open the database
//DBNAME is the name of the database
//null:- CursorFactory(Interface) its creating the cursor object .
//default value of object is null.
// version of the database.
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLENAME+"("+NAME+" text,"+MNAME+" text, "+MNO+" text,"+EMAIL+" text)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
STEP 3:- Now Fetch Data from activity_main.xml to MainActivity.java.
:-Edit MainActivity.java based on following code.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
SQLiteStatement statement;
MyDbHelper dbHelper;
SQLiteDatabase database;
String Name,Lname,Mob,Email;
EditText name,lname,mob,email;
Button b;
Button login;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create the database connection and make it as writeable database.
dbHelper=new MyDbHelper(this);
database=dbHelper.getWritableDatabase();
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText1);
lname=(EditText)findViewById(R.id.editText2);
email=(EditText)findViewById(R.id.editText3);
mob=(EditText)findViewById(R.id.editText4);
login=(Button)findViewById(R.id.button2);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent log=new Intent(MainActivity.this,LoginActivity.class);
startActivity(log);
}
});
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// convert the data into the string
Name=name.getText().toString();
Lname=lname.getText().toString();
Email=email.getText().toString();
Mob=mob.getText().toString();
try{
//now compile the statement and apply the insert query.
statement=database.compileStatement("insert into "+MyDbHelper.TABLENAME+" values(?,?,?,?)" );
statement.bindString(1, Name);
statement.bindString(2, Lname);
statement.bindString(3, Email);
statement.bindString(4, Mob);
statement.executeInsert();
Toast.makeText(MainActivity.this, "Register succefully done", Toast.LENGTH_LONG).show();
//if the data is successfully inserted than you get the toast and redirect to login screen. Intent i=new Intent(MainActivity.this,LoginActivity.class);
startActivity(i);
}
catch(Exception e){
Toast.makeText(MainActivity.this, "database exception", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
SQLiteStatement statement;
MyDbHelper dbHelper;
SQLiteDatabase database;
String Name,Lname,Mob,Email;
EditText name,lname,mob,email;
Button b;
Button login;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create the database connection and make it as writeable database.
dbHelper=new MyDbHelper(this);
database=dbHelper.getWritableDatabase();
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText1);
lname=(EditText)findViewById(R.id.editText2);
email=(EditText)findViewById(R.id.editText3);
mob=(EditText)findViewById(R.id.editText4);
login=(Button)findViewById(R.id.button2);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent log=new Intent(MainActivity.this,LoginActivity.class);
startActivity(log);
}
});
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// convert the data into the string
Name=name.getText().toString();
Lname=lname.getText().toString();
Email=email.getText().toString();
Mob=mob.getText().toString();
try{
//now compile the statement and apply the insert query.
statement=database.compileStatement("insert into "+MyDbHelper.TABLENAME+" values(?,?,?,?)" );
statement.bindString(1, Name);
statement.bindString(2, Lname);
statement.bindString(3, Email);
statement.bindString(4, Mob);
statement.executeInsert();
Toast.makeText(MainActivity.this, "Register succefully done", Toast.LENGTH_LONG).show();
//if the data is successfully inserted than you get the toast and redirect to login screen. Intent i=new Intent(MainActivity.this,LoginActivity.class);
startActivity(i);
}
catch(Exception e){
Toast.makeText(MainActivity.this, "database exception", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
STEP:4 Run application on Your Emulator.You will get the out put.
NOTE:- IF you have any question regard android & java you can comment here i will give feedback to you .
:-learn java :-http://www.javawithjnext.blogspot.com
No comments:
Post a Comment