Output of
tabhost with database insert delete update...etc

1..insert
2..delete
3.update


4.view
XML files...

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
insert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="Name" android:id="@+id/textView2" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtNM" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="DESIG" android:id="@+id/textView3" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtDESIG" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="phone" android:id="@+id/textView1" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtPHONE" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<Button android:layout_height="wrap_content" android:text="Save" android:id="@+id/button1" android:layout_width="76dp"></Button>
<Button android:text="Clear" android:id="@+id/btnClr" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
detele.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Enter the name u want to delete" android:layout_width="wrap_content"></TextView>
<EditText android:id="@+id/edtdelete" android:hint="enter name to delete" android:layout_height="wrap_content" android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Delete" android:id="@+id/btnDelete" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
update.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="wrap_content" android:text="ID" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtId" android:layout_width="wrap_content" android:hint="Enter id to update" android:layout_weight="0.95">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<Button android:text="Search" android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:text="Name" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtName" android:layout_weight="1" android:layout_width="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_width="wrap_content" android:text="Desig" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/edtDesig" android:layout_width="match_parent"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView4" android:layout_width="wrap_content" android:text="Phone" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtPhone" android:layout_weight="1" android:layout_width="wrap_content" android:inputType="phone"></EditText>
</LinearLayout>
<Button android:text="Update" android:id="@+id/btnUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
JavaFiles....

TabHostActivity.java
package com.abc;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class TabhostActivity extends TabActivity implements TabContentFactory {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost th=getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Insert").setContent(new Intent(this,Insert1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Delete").setContent(new Intent(this,Delete1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Update").setContent(new Intent(this,Update1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("View").setContent(new Intent(this,View1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return null;
}
}
Insert1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.Contacts.Intents.Insert;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Insert1 extends Activity implements OnClickListener {
EditText Vname,Vdesig,Vphone;
Button save;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
Vname=(EditText)findViewById(R.id.edtNM);
Vdesig=(EditText)findViewById(R.id.edtDESIG);
Vphone=(EditText)findViewById(R.id.edtPHONE);
save=(Button)findViewById(R.id.button1);
//save.setOnClickListener(this);
save.setOnClickListener(this);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
String sql="create table IF NOT EXISTS employee(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,desig TEXT,phone TEXT)";
db.execSQL(sql);
}
@Override
public void onClick(View v) {
ContentValues cv=new ContentValues();
cv.put("name",Vname.getText().toString());
cv.put("desig",Vdesig.getText().toString());
cv.put("phone",Vphone.getText().toString());
db.insert("employee", null, cv);
Dialog d=new Dialog(this);
d.setTitle("confirm");
TextView tv=new TextView(this);
tv.setText("successfully added...");
d.setContentView(tv);
d.show();
//db.close();
}
public void onDestroy()
{
super.onDestroy();
db.close();
}
}
Delete1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
public class Delete1 extends Activity {
EditText Vedtdelete;
Button Vbtndelete;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete);
Vedtdelete=(EditText)findViewById(R.id.edtdelete);
Vbtndelete=(Button)findViewById(R.id.btnDelete);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
Vbtndelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
String sql="delete from employee where name='"+Vedtdelete.getText().toString()+"'";
db.execSQL(sql);
//db.delete("employee", "name="+Vedtdelete.getText().toString(),null );
}
catch(Exception e)
{
TextView t = new TextView(Delete1.this);
t.setText(e.toString());
Dialog d= new Dialog(Delete1.this);
d.setTitle("error");
d.setContentView(t);
d.show();
}
db.close();
}
});
}
/* public void onDestroy()
{
db.close();
}*/
}
Update1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
import android.widget.Toast;
public class Update1 extends Activity
{
/** Called when the activity is first created. */
Button vupdate,vsearch;
EditText vid,vnm,vdesig,vphone;
String eid="0";
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
vupdate=(Button)findViewById(R.id.btnUpdate);
vsearch=(Button)findViewById(R.id.btnSearch);
vid=(EditText)findViewById(R.id.edtId);
vnm=(EditText)findViewById(R.id.edtName);
vdesig=(EditText)findViewById(R.id.edtDesig);
vphone=(EditText)findViewById(R.id.edtPhone);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
//vnm.setEnabled(false);
//vdesig.setEnabled(false);
//vphone.setEnabled(false);
// vupdate.setEnabled(false);
vsearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=db.rawQuery("select name,desig,phone from employee where id="+vid.getText().toString(),null);
c.moveToFirst();
String s=c.getString(0);
vnm.setText(s);
vdesig.setText(c.getString(1));
vphone.setText(c.getString(2));
/*Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText(s);
d.setContentView(tv);
d.show();*/
c.close();
}
});
vupdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//vupdate.setEnabled(true);
//vnm.setEnabled(true);
//vdesig.setEnabled(true);
// vphone.setEnabled(true);
String sql="update employee set name='"+vnm.getText().toString()+"',desig='"+vdesig.getText().toString()+"',phone='"+vphone.getText().toString()+"' where id="+vid.getText().toString()+"";
db.execSQL(sql);
Toast.makeText(getApplicationContext(), "Successfully updated...", Toast.LENGTH_LONG).show();
/* Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText("successfully updated...");
d.setContentView(tv);
d.show();*/
// vnm.setText("");
//vdesig.setText("");
//vphone.setText("");
//vnm.requestFocus();
}
});
}
}
View1.java
package com.abc;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class View1 extends ListActivity {
SQLiteDatabase db;
Cursor c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READONLY, null);
c=db.rawQuery("select * from employee",null);
ArrayList<String> list =new ArrayList<String>();
c.moveToFirst();
//if(c.getCount()>0)
while(c.moveToNext())
{
list.add(c.getString(0)+","+c.getString(1)+","+c.getString(2)+","+c.getString(3));
}
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
getListView().setAdapter(ad);
c.close();
db.close();
// TextView tv=new TextView(View1.this);
//tv.setText(e.toString());
}
/*public void onDestroy()
{
c.close();
db.close();
}*/
}
2
Output of
tabhost with database insert delete update...etc

1..insert
2..delete
3.update


4.view
XML files...

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
insert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="Name" android:id="@+id/textView2" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtNM" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="DESIG" android:id="@+id/textView3" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtDESIG" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="phone" android:id="@+id/textView1" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtPHONE" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<Button android:layout_height="wrap_content" android:text="Save" android:id="@+id/button1" android:layout_width="76dp"></Button>
<Button android:text="Clear" android:id="@+id/btnClr" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
detele.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Enter the name u want to delete" android:layout_width="wrap_content"></TextView>
<EditText android:id="@+id/edtdelete" android:hint="enter name to delete" android:layout_height="wrap_content" android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Delete" android:id="@+id/btnDelete" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
update.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="wrap_content" android:text="ID" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtId" android:layout_width="wrap_content" android:hint="Enter id to update" android:layout_weight="0.95">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<Button android:text="Search" android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:text="Name" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtName" android:layout_weight="1" android:layout_width="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_width="wrap_content" android:text="Desig" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/edtDesig" android:layout_width="match_parent"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView4" android:layout_width="wrap_content" android:text="Phone" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtPhone" android:layout_weight="1" android:layout_width="wrap_content" android:inputType="phone"></EditText>
</LinearLayout>
<Button android:text="Update" android:id="@+id/btnUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
JavaFiles....

TabHostActivity.java
package com.abc;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class TabhostActivity extends TabActivity implements TabContentFactory {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost th=getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Insert").setContent(new Intent(this,Insert1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Delete").setContent(new Intent(this,Delete1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Update").setContent(new Intent(this,Update1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("View").setContent(new Intent(this,View1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return null;
}
}
Insert1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.Contacts.Intents.Insert;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Insert1 extends Activity implements OnClickListener {
EditText Vname,Vdesig,Vphone;
Button save;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
Vname=(EditText)findViewById(R.id.edtNM);
Vdesig=(EditText)findViewById(R.id.edtDESIG);
Vphone=(EditText)findViewById(R.id.edtPHONE);
save=(Button)findViewById(R.id.button1);
//save.setOnClickListener(this);
save.setOnClickListener(this);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
String sql="create table IF NOT EXISTS employee(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,desig TEXT,phone TEXT)";
db.execSQL(sql);
}
@Override
public void onClick(View v) {
ContentValues cv=new ContentValues();
cv.put("name",Vname.getText().toString());
cv.put("desig",Vdesig.getText().toString());
cv.put("phone",Vphone.getText().toString());
db.insert("employee", null, cv);
Dialog d=new Dialog(this);
d.setTitle("confirm");
TextView tv=new TextView(this);
tv.setText("successfully added...");
d.setContentView(tv);
d.show();
//db.close();
}
public void onDestroy()
{
super.onDestroy();
db.close();
}
}
Delete1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
public class Delete1 extends Activity {
EditText Vedtdelete;
Button Vbtndelete;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete);
Vedtdelete=(EditText)findViewById(R.id.edtdelete);
Vbtndelete=(Button)findViewById(R.id.btnDelete);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
Vbtndelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
String sql="delete from employee where name='"+Vedtdelete.getText().toString()+"'";
db.execSQL(sql);
//db.delete("employee", "name="+Vedtdelete.getText().toString(),null );
}
catch(Exception e)
{
TextView t = new TextView(Delete1.this);
t.setText(e.toString());
Dialog d= new Dialog(Delete1.this);
d.setTitle("error");
d.setContentView(t);
d.show();
}
db.close();
}
});
}
/* public void onDestroy()
{
db.close();
}*/
}
Update1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
import android.widget.Toast;
public class Update1 extends Activity
{
/** Called when the activity is first created. */
Button vupdate,vsearch;
EditText vid,vnm,vdesig,vphone;
String eid="0";
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
vupdate=(Button)findViewById(R.id.btnUpdate);
vsearch=(Button)findViewById(R.id.btnSearch);
vid=(EditText)findViewById(R.id.edtId);
vnm=(EditText)findViewById(R.id.edtName);
vdesig=(EditText)findViewById(R.id.edtDesig);
vphone=(EditText)findViewById(R.id.edtPhone);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
//vnm.setEnabled(false);
//vdesig.setEnabled(false);
//vphone.setEnabled(false);
// vupdate.setEnabled(false);
vsearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=db.rawQuery("select name,desig,phone from employee where id="+vid.getText().toString(),null);
c.moveToFirst();
String s=c.getString(0);
vnm.setText(s);
vdesig.setText(c.getString(1));
vphone.setText(c.getString(2));
/*Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText(s);
d.setContentView(tv);
d.show();*/
c.close();
}
});
vupdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//vupdate.setEnabled(true);
//vnm.setEnabled(true);
//vdesig.setEnabled(true);
// vphone.setEnabled(true);
String sql="update employee set name='"+vnm.getText().toString()+"',desig='"+vdesig.getText().toString()+"',phone='"+vphone.getText().toString()+"' where id="+vid.getText().toString()+"";
db.execSQL(sql);
Toast.makeText(getApplicationContext(), "Successfully updated...", Toast.LENGTH_LONG).show();
/* Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText("successfully updated...");
d.setContentView(tv);
d.show();*/
// vnm.setText("");
//vdesig.setText("");
//vphone.setText("");
//vnm.requestFocus();
}
});
}
}
View1.java
package com.abc;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class View1 extends ListActivity {
SQLiteDatabase db;
Cursor c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READONLY, null);
c=db.rawQuery("select * from employee",null);
ArrayList<String> list =new ArrayList<String>();
c.moveToFirst();
//if(c.getCount()>0)
while(c.moveToNext())
{
list.add(c.getString(0)+","+c.getString(1)+","+c.getString(2)+","+c.getString(3));
}
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
getListView().setAdapter(ad);
c.close();
db.close();
// TextView tv=new TextView(View1.this);
//tv.setText(e.toString());
}
/*public void onDestroy()
{
c.close();
db.close();
}*/
}

1..insert

2..delete

3.update



4.view

XML files...

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
insert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="Name" android:id="@+id/textView2" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtNM" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="DESIG" android:id="@+id/textView3" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtDESIG" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="phone" android:id="@+id/textView1" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtPHONE" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<Button android:layout_height="wrap_content" android:text="Save" android:id="@+id/button1" android:layout_width="76dp"></Button>
<Button android:text="Clear" android:id="@+id/btnClr" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
detele.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Enter the name u want to delete" android:layout_width="wrap_content"></TextView>
<EditText android:id="@+id/edtdelete" android:hint="enter name to delete" android:layout_height="wrap_content" android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Delete" android:id="@+id/btnDelete" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
update.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="wrap_content" android:text="ID" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtId" android:layout_width="wrap_content" android:hint="Enter id to update" android:layout_weight="0.95">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<Button android:text="Search" android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:text="Name" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtName" android:layout_weight="1" android:layout_width="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_width="wrap_content" android:text="Desig" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/edtDesig" android:layout_width="match_parent"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView4" android:layout_width="wrap_content" android:text="Phone" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtPhone" android:layout_weight="1" android:layout_width="wrap_content" android:inputType="phone"></EditText>
</LinearLayout>
<Button android:text="Update" android:id="@+id/btnUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
JavaFiles....

TabHostActivity.java
package com.abc;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class TabhostActivity extends TabActivity implements TabContentFactory {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost th=getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Insert").setContent(new Intent(this,Insert1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Delete").setContent(new Intent(this,Delete1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Update").setContent(new Intent(this,Update1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("View").setContent(new Intent(this,View1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return null;
}
}
Insert1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.Contacts.Intents.Insert;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Insert1 extends Activity implements OnClickListener {
EditText Vname,Vdesig,Vphone;
Button save;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
Vname=(EditText)findViewById(R.id.edtNM);
Vdesig=(EditText)findViewById(R.id.edtDESIG);
Vphone=(EditText)findViewById(R.id.edtPHONE);
save=(Button)findViewById(R.id.button1);
//save.setOnClickListener(this);
save.setOnClickListener(this);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
String sql="create table IF NOT EXISTS employee(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,desig TEXT,phone TEXT)";
db.execSQL(sql);
}
@Override
public void onClick(View v) {
ContentValues cv=new ContentValues();
cv.put("name",Vname.getText().toString());
cv.put("desig",Vdesig.getText().toString());
cv.put("phone",Vphone.getText().toString());
db.insert("employee", null, cv);
Dialog d=new Dialog(this);
d.setTitle("confirm");
TextView tv=new TextView(this);
tv.setText("successfully added...");
d.setContentView(tv);
d.show();
//db.close();
}
public void onDestroy()
{
super.onDestroy();
db.close();
}
}
Delete1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
public class Delete1 extends Activity {
EditText Vedtdelete;
Button Vbtndelete;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete);
Vedtdelete=(EditText)findViewById(R.id.edtdelete);
Vbtndelete=(Button)findViewById(R.id.btnDelete);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
Vbtndelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
String sql="delete from employee where name='"+Vedtdelete.getText().toString()+"'";
db.execSQL(sql);
//db.delete("employee", "name="+Vedtdelete.getText().toString(),null );
}
catch(Exception e)
{
TextView t = new TextView(Delete1.this);
t.setText(e.toString());
Dialog d= new Dialog(Delete1.this);
d.setTitle("error");
d.setContentView(t);
d.show();
}
db.close();
}
});
}
/* public void onDestroy()
{
db.close();
}*/
}
Update1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
import android.widget.Toast;
public class Update1 extends Activity
{
/** Called when the activity is first created. */
Button vupdate,vsearch;
EditText vid,vnm,vdesig,vphone;
String eid="0";
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
vupdate=(Button)findViewById(R.id.btnUpdate);
vsearch=(Button)findViewById(R.id.btnSearch);
vid=(EditText)findViewById(R.id.edtId);
vnm=(EditText)findViewById(R.id.edtName);
vdesig=(EditText)findViewById(R.id.edtDesig);
vphone=(EditText)findViewById(R.id.edtPhone);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
//vnm.setEnabled(false);
//vdesig.setEnabled(false);
//vphone.setEnabled(false);
// vupdate.setEnabled(false);
vsearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=db.rawQuery("select name,desig,phone from employee where id="+vid.getText().toString(),null);
c.moveToFirst();
String s=c.getString(0);
vnm.setText(s);
vdesig.setText(c.getString(1));
vphone.setText(c.getString(2));
/*Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText(s);
d.setContentView(tv);
d.show();*/
c.close();
}
});
vupdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//vupdate.setEnabled(true);
//vnm.setEnabled(true);
//vdesig.setEnabled(true);
// vphone.setEnabled(true);
String sql="update employee set name='"+vnm.getText().toString()+"',desig='"+vdesig.getText().toString()+"',phone='"+vphone.getText().toString()+"' where id="+vid.getText().toString()+"";
db.execSQL(sql);
Toast.makeText(getApplicationContext(), "Successfully updated...", Toast.LENGTH_LONG).show();
/* Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText("successfully updated...");
d.setContentView(tv);
d.show();*/
// vnm.setText("");
//vdesig.setText("");
//vphone.setText("");
//vnm.requestFocus();
}
});
}
}
View1.java
package com.abc;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class View1 extends ListActivity {
SQLiteDatabase db;
Cursor c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READONLY, null);
c=db.rawQuery("select * from employee",null);
ArrayList<String> list =new ArrayList<String>();
c.moveToFirst();
//if(c.getCount()>0)
while(c.moveToNext())
{
list.add(c.getString(0)+","+c.getString(1)+","+c.getString(2)+","+c.getString(3));
}
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
getListView().setAdapter(ad);
c.close();
db.close();
// TextView tv=new TextView(View1.this);
//tv.setText(e.toString());
}
/*public void onDestroy()
{
c.close();
db.close();
}*/
}

1..insert

2..delete

3.update



4.view

XML files...

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
insert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="Name" android:id="@+id/textView2" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtNM" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="DESIG" android:id="@+id/textView3" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtDESIG" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:text="phone" android:id="@+id/textView1" android:layout_width="wrap_content" android:textSize="20dp" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/edtPHONE" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<Button android:layout_height="wrap_content" android:text="Save" android:id="@+id/button1" android:layout_width="76dp"></Button>
<Button android:text="Clear" android:id="@+id/btnClr" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
detele.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Enter the name u want to delete" android:layout_width="wrap_content"></TextView>
<EditText android:id="@+id/edtdelete" android:hint="enter name to delete" android:layout_height="wrap_content" android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Delete" android:id="@+id/btnDelete" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
update.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="wrap_content" android:text="ID" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtId" android:layout_width="wrap_content" android:hint="Enter id to update" android:layout_weight="0.95">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<Button android:text="Search" android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:text="Name" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtName" android:layout_weight="1" android:layout_width="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_width="wrap_content" android:text="Desig" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/edtDesig" android:layout_width="match_parent"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/textView4" android:layout_width="wrap_content" android:text="Phone" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/edtPhone" android:layout_weight="1" android:layout_width="wrap_content" android:inputType="phone"></EditText>
</LinearLayout>
<Button android:text="Update" android:id="@+id/btnUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
JavaFiles....

TabHostActivity.java
package com.abc;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class TabhostActivity extends TabActivity implements TabContentFactory {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost th=getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Insert").setContent(new Intent(this,Insert1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Delete").setContent(new Intent(this,Delete1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("Update").setContent(new Intent(this,Update1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
th.addTab(th.newTabSpec("tab1").setIndicator("View").setContent(new Intent(this,View1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return null;
}
}
Insert1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.Contacts.Intents.Insert;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Insert1 extends Activity implements OnClickListener {
EditText Vname,Vdesig,Vphone;
Button save;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
Vname=(EditText)findViewById(R.id.edtNM);
Vdesig=(EditText)findViewById(R.id.edtDESIG);
Vphone=(EditText)findViewById(R.id.edtPHONE);
save=(Button)findViewById(R.id.button1);
//save.setOnClickListener(this);
save.setOnClickListener(this);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
String sql="create table IF NOT EXISTS employee(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,desig TEXT,phone TEXT)";
db.execSQL(sql);
}
@Override
public void onClick(View v) {
ContentValues cv=new ContentValues();
cv.put("name",Vname.getText().toString());
cv.put("desig",Vdesig.getText().toString());
cv.put("phone",Vphone.getText().toString());
db.insert("employee", null, cv);
Dialog d=new Dialog(this);
d.setTitle("confirm");
TextView tv=new TextView(this);
tv.setText("successfully added...");
d.setContentView(tv);
d.show();
//db.close();
}
public void onDestroy()
{
super.onDestroy();
db.close();
}
}
Delete1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
public class Delete1 extends Activity {
EditText Vedtdelete;
Button Vbtndelete;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete);
Vedtdelete=(EditText)findViewById(R.id.edtdelete);
Vbtndelete=(Button)findViewById(R.id.btnDelete);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
Vbtndelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
String sql="delete from employee where name='"+Vedtdelete.getText().toString()+"'";
db.execSQL(sql);
//db.delete("employee", "name="+Vedtdelete.getText().toString(),null );
}
catch(Exception e)
{
TextView t = new TextView(Delete1.this);
t.setText(e.toString());
Dialog d= new Dialog(Delete1.this);
d.setTitle("error");
d.setContentView(t);
d.show();
}
db.close();
}
});
}
/* public void onDestroy()
{
db.close();
}*/
}
Update1.java
package com.abc;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
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.TextView;
import android.widget.Toast;
public class Update1 extends Activity
{
/** Called when the activity is first created. */
Button vupdate,vsearch;
EditText vid,vnm,vdesig,vphone;
String eid="0";
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
vupdate=(Button)findViewById(R.id.btnUpdate);
vsearch=(Button)findViewById(R.id.btnSearch);
vid=(EditText)findViewById(R.id.edtId);
vnm=(EditText)findViewById(R.id.edtName);
vdesig=(EditText)findViewById(R.id.edtDesig);
vphone=(EditText)findViewById(R.id.edtPhone);
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READWRITE,null);
//vnm.setEnabled(false);
//vdesig.setEnabled(false);
//vphone.setEnabled(false);
// vupdate.setEnabled(false);
vsearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=db.rawQuery("select name,desig,phone from employee where id="+vid.getText().toString(),null);
c.moveToFirst();
String s=c.getString(0);
vnm.setText(s);
vdesig.setText(c.getString(1));
vphone.setText(c.getString(2));
/*Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText(s);
d.setContentView(tv);
d.show();*/
c.close();
}
});
vupdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//vupdate.setEnabled(true);
//vnm.setEnabled(true);
//vdesig.setEnabled(true);
// vphone.setEnabled(true);
String sql="update employee set name='"+vnm.getText().toString()+"',desig='"+vdesig.getText().toString()+"',phone='"+vphone.getText().toString()+"' where id="+vid.getText().toString()+"";
db.execSQL(sql);
Toast.makeText(getApplicationContext(), "Successfully updated...", Toast.LENGTH_LONG).show();
/* Dialog d=new Dialog(Update.this);
d.setTitle("confirm");
TextView tv=new TextView(Update.this);
tv.setText("successfully updated...");
d.setContentView(tv);
d.show();*/
// vnm.setText("");
//vdesig.setText("");
//vphone.setText("");
//vnm.requestFocus();
}
});
}
}
View1.java
package com.abc;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class View1 extends ListActivity {
SQLiteDatabase db;
Cursor c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
db=openOrCreateDatabase("EMS.db",SQLiteDatabase.OPEN_READONLY, null);
c=db.rawQuery("select * from employee",null);
ArrayList<String> list =new ArrayList<String>();
c.moveToFirst();
//if(c.getCount()>0)
while(c.moveToNext())
{
list.add(c.getString(0)+","+c.getString(1)+","+c.getString(2)+","+c.getString(3));
}
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
getListView().setAdapter(ad);
c.close();
db.close();
// TextView tv=new TextView(View1.this);
//tv.setText(e.toString());
}
/*public void onDestroy()
{
c.close();
db.close();
}*/
}
Comments
Post a Comment