If you implement this code to manage simple app this will make you helpful .......
User Interfaces are as:-
1:-
<?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="wrap_content"
android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView1" android:text="Employee App"
android:layout_gravity="center"></TextView>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtEname"
android:hint="Enter Employee Name">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtDesig"
android:hint="Enter Designation"></EditText>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtSalary"
android:hint="Enter Salary"></EditText>
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/linearLayout1" android:layout_width="match_parent">
<Button android:id="@+id/btnAdd" android:text="Add Employee"
android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/btnUpdate"
android:text="Update Record"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/btnDelete"
android:text="Delete "></Button>
</LinearLayout>
<ListView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/lvEmployees"></ListView>
</LinearLayout>
User Interfaces are as:-
1:-
<?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="wrap_content"
android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView1" android:text="Employee App"
android:layout_gravity="center"></TextView>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtEname"
android:hint="Enter Employee Name">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtDesig"
android:hint="Enter Designation"></EditText>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtSalary"
android:hint="Enter Salary"></EditText>
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/linearLayout1" android:layout_width="match_parent">
<Button android:id="@+id/btnAdd" android:text="Add Employee"
android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/btnUpdate"
android:text="Update Record"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/btnDelete"
android:text="Delete "></Button>
</LinearLayout>
<ListView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/lvEmployees"></ListView>
</LinearLayout>
2:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:id="@+id/lblEname"></TextView>
<TextView android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentRight="true"
android:id="@+id/lblSalary"></TextView>
<TextView android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" android:id="@+id/lblDesignation"
android:layout_width="wrap_content" android:layout_below="@+id/lblSalary"
android:layout_alignParentLeft="true"></TextView>
</RelativeLayout>
Manifest:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vrs.employeeapp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EmployeeAppActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity:-
package com.vrs.employeeapp;
/***
* Application Name : MessageBox
* Author : Vimal Rughani
* Website : http://pulse7.net
* For more details visit http://pulse7.net/android/sqlite-database-android/
*/
import android.app.Activity;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class EmployeeAppActivity extends Activity implements OnClickListener {
// Primitive Variables
String selected_ID = "";
// Widget GUI Declare
EditText txtEname, txtDesig, txtSalary;
Button btnAddEmployee, btnUpdate, btnDelete;
ListView lvEmployees;
// DB Objects
DBHelper helper;
SQLiteDatabase db;
// Adapter Object
SimpleCursorAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Init DB Objects
helper = new DBHelper(this);
// Widget GUI Init
txtEname = (EditText) findViewById(R.id.txtEname);
txtDesig = (EditText) findViewById(R.id.txtDesig);
txtSalary = (EditText) findViewById(R.id.txtSalary);
lvEmployees = (ListView) findViewById(R.id.lvEmployees);
btnAddEmployee = (Button) findViewById(R.id.btnAdd);
btnUpdate = (Button) findViewById(R.id.btnUpdate);
btnDelete = (Button) findViewById(R.id.btnDelete);
// Attached Listener
btnAddEmployee.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
btnDelete.setOnClickListener(this);
lvEmployees.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
String name, desig, salary;
// Display Selected Row of Listview into EditText widget
Cursor row = (Cursor) adapter.getItemAtPosition(position);
selected_ID = row.getString(0);
name = row.getString(1);
desig = row.getString(2);
salary = row.getString(3);
txtEname.setText(name);
txtDesig.setText(desig);
txtSalary.setText(salary);
}
});
// Fetch Data from database
fetchData();
}
@Override
public void onClick(View v) {
// Perform CRUD Operation
if (v == btnAddEmployee) {
// Add Record with help of ContentValues and DBHelper class object
ContentValues values = new ContentValues();
values.put(DBHelper.C_ENAME, txtEname.getText().toString());
values.put(DBHelper.C_DESIGNATION, txtDesig.getText().toString());
values.put(DBHelper.C_SALARY, txtSalary.getText().toString());
// Call insert method of SQLiteDatabase Class and close after
// performing task
db = helper.getWritableDatabase();
db.insert(DBHelper.TABLE, null, values);
db.close();
clearFields();
Toast.makeText(this, "Employee Added Successfully",
Toast.LENGTH_LONG).show();
// Fetch Data from database and display into listview
fetchData();
}
if (v == btnUpdate) {
// Update Record with help of ContentValues and DBHelper class
// object
ContentValues values = new ContentValues();
values.put(DBHelper.C_ENAME, txtEname.getText().toString());
values.put(DBHelper.C_DESIGNATION, txtDesig.getText().toString());
values.put(DBHelper.C_SALARY, txtSalary.getText().toString());
// Call update method of SQLiteDatabase Class and close after
// performing task
db = helper.getWritableDatabase();
db.update(DBHelper.TABLE, values, DBHelper.C_ID + "=?",
new String[] { selected_ID });
db.close();
// Fetch Data from database and display into listview
fetchData();
Toast.makeText(this, "Record Updated Successfully",
Toast.LENGTH_LONG).show();
clearFields();
}
if (v == btnDelete) {
// Call delete method of SQLiteDatabase Class to delete record and
// close after performing task
db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE, DBHelper.C_ID + "=?",
new String[] { selected_ID });
db.close();
// Fetch Data from database and display into listview
fetchData();
Toast.makeText(this, "Record Deleted Successfully",
Toast.LENGTH_LONG).show();
clearFields();
}
}
// Clear Fields
private void clearFields() {
txtEname.setText("");
txtDesig.setText("");
txtSalary.setText("");
}
// Fetch Fresh data from database and display into listview
private void fetchData() {
db = helper.getReadableDatabase();
Cursor c = db.query(DBHelper.TABLE, null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.row,
c,
new String[] { DBHelper.C_ENAME, DBHelper.C_SALARY,
DBHelper.C_DESIGNATION },
new int[] { R.id.lblEname, R.id.lblSalary, R.id.lblDesignation });
lvEmployees.setAdapter(adapter);
}
}
SQLiteOpenHelper Class Extends:-
package com.vrs.employeeapp;
/***
* Application Name : MessageBox
* Author : Vimal Rughani
* Website : http://pulse7.net
* For more details visit http://pulse7.net/android/sqlite-database-android/
*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
// Static Final Variable database meta information
static final String DATABASE = "empapp.db";
static final int VERSION = 1;
static final String TABLE = "emp";
static final String C_ID = "_id";
static final String C_ENAME = "ename";
static final String C_DESIGNATION = "designation";
static final String C_SALARY = "salary";
// Override constructor
public DBHelper(Context context) {
super(context, DATABASE, null, VERSION);
}
// Override onCreate method
@Override
public void onCreate(SQLiteDatabase db) {
// Create Employee table with following fields
// _ID, ENAME, DESIGNATION and SALARY
db.execSQL("CREATE TABLE " + TABLE + " ( " + C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_ENAME + " text, "
+ C_DESIGNATION + " text, " + C_SALARY + " text )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop old version table
db.execSQL("Drop table " + TABLE);
// Create New Version table
onCreate(db);
}
}
Supportive
package com.vrs.employeeapp;
public interface OnItemClickListener extends
android.widget.AdapterView.OnItemClickListener {
}
Thanks to All My Friends
This comment has been removed by the author.
ReplyDelete