· 6 years ago · Jun 14, 2019, 06:26 AM
1package com.example.employeesapp2;
2
3//import android.content.Intent;
4import android.database.Cursor;
5import android.database.sqlite.SQLiteDatabase;
6import android.support.v7.app.AlertDialog;
7import android.support.v7.app.AppCompatActivity;
8import android.os.Bundle;
9import android.view.View;
10import android.widget.Button;
11import android.widget.EditText;
12
13public class MainActivity extends AppCompatActivity {
14 //Declaring variable for the database
15 SQLiteDatabase db;
16 //Declaring View Variables
17 EditText editsearchname, editempname, editempmail, editempsalary;
18 Button Add, Delete, Modify, View, search;
19
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.activity_main);
24
25 //Initializing View Variables from activity_main.xml file
26 editsearchname = (EditText) findViewById(R.id.edtemployeename);
27 editempname = (EditText) findViewById(R.id.editText);
28 editempmail = (EditText) findViewById(R.id.editText2);
29 editempsalary = (EditText) findViewById(R.id.editText3);
30 Add = (Button) findViewById(R.id.btnsave);
31 Delete = (Button) findViewById(R.id.btndel);
32 Modify = (Button) findViewById(R.id.btnupdate);
33 View = (Button) findViewById(R.id.btnselect);
34 search = (Button) findViewById(R.id.btnselectperticular);
35
36 Add.setOnClickListener(new View.OnClickListener() {
37 @Override
38 public void onClick(android.view.View v) {
39 //Check if user has filled in all the values
40
41 if (editempname.getText().toString().trim().length()==0){
42 errorMessage("NAME ERROR ","Kindly fill in your name ") ;
43 }else if (editempmail.getText().toString().length()==0) {
44 errorMessage("EMAIL ERROr", "Please enter your email Address");
45 }else if (editempsalary.getText().toString().length()==0){
46 errorMessage("ID ERROR","Kindly enter your ID");
47 }
48
49 else {
50 //insert data to DB
51 db.execSQL("INSERT INTO Employee VALUES('"+editempname.getText()+"','"+editempmail.getText()+"','"+editempsalary.getText()+"')");
52 errorMessage("QUERY SUCCESS","Data was succefully saved");
53 clear();
54 }
55 }
56 });
57
58 Modify.setOnClickListener(new View.OnClickListener() {
59 @Override
60 public void onClick(android.view.View v) {
61 //code for update data
62 if(editsearchname.getText().toString().trim().length()==0)
63 {
64 errorMessage("SEARCH", "Enter Employee Name");
65 return;
66 }
67 Cursor c=db.rawQuery("SELECT * FROM Employee WHERE EmpName='"+ editsearchname.getText()+"'", null);
68 if(c.moveToFirst()) {
69 db.execSQL("UPDATE Employee SET EmpName ='"+ editempname.getText()+"', EmpMail='"+ editempmail.getText()+"',EmpSalary='"+ editempsalary.getText()+"' WHERE EmpName ='"+editsearchname.getText()+"'");
70 errorMessage("SUCCESS", "Record Modified");
71 }
72 else
73 {
74 errorMessage("ERROR", "Invalid Employee Name");
75 }
76
77 }
78 });
79
80 Delete.setOnClickListener(new View.OnClickListener() {
81 @Override
82 public void onClick(android.view.View v) {
83 //code for delete data
84 if(editsearchname.getText().toString().trim().length()==0)
85 {
86 errorMessage("INPUT", " Please enter Employee Name ");
87 return;
88 }
89 Cursor c=db.rawQuery("SELECT * FROM Employee WHERE EmpName ='"+ editsearchname.getText()+"'", null);
90 if(c.moveToFirst())
91 {
92 db.execSQL("DELETE FROM Employee WHERE EmpName ='"+ editsearchname.getText()+"'");
93 errorMessage("SUCCESS", "Record Deleted");
94 }
95 else
96 {
97 errorMessage("ERROR", "Invalid Employee Name ");
98 }
99
100 }
101 });
102
103 View.setOnClickListener(new View.OnClickListener() {
104 @Override
105 public void onClick(android.view.View v) {
106 //code for select all data
107 Cursor c=db.rawQuery("SELECT * FROM Employee", null);
108 if(c.getCount()==0)
109 {
110 errorMessage("ERROR", "No records found");
111 return;
112 }
113 StringBuffer buffer=new StringBuffer();
114 while(c.moveToNext())
115 {
116 buffer.append("Employee Name: "+c.getString(1)+"\n");
117 buffer.append("Employee Mail: "+c.getString(2)+"\n\n");
118 buffer.append("Employee Salary: "+c.getString(3)+"\n\n");
119 }
120 errorMessage("SUCCESS", buffer.toString());
121
122 }
123 });
124
125 search.setOnClickListener(new View.OnClickListener() {
126 @Override
127 public void onClick(android.view.View v) {
128 //code for select particular data
129 if(editsearchname.getText().toString().trim().length()==0)
130 {
131 errorMessage("INPUT", "Enter Employee Name");
132 return;
133 }
134 Cursor c=db.rawQuery("SELECT * FROM Employee WHERE EmpName='"+editsearchname.getText()+"'", null);
135 if(c.moveToFirst())
136 {
137 editempname.setText(c.getString(1));
138 editempmail.setText(c.getString(2));
139 editempsalary.setText(c.getString(3));
140 }
141 else
142 {
143 errorMessage("ERROR", "Invalid Employee Name");
144 }
145 }
146 });
147
148 //Method for creating a database(Create/View)
149
150 db = openOrCreateDatabase("EmployeesDB", MODE_PRIVATE,null);
151
152 // Query to create a TABLE with three columns
153
154 db.execSQL("CREATE TABLE IF NOT EXISTS Employee(EmpId INTEGER PRIMARY KEY AUTOINCREMENT," +
155 "EmpName VARCHAR,EmpMail VARCHAR,EmpSalary VARCHAR);");
156
157
158 //save data to DB
159
160 }
161 //Error message display
162 private void errorMessage(String title, String message) {
163 AlertDialog.Builder builder = new AlertDialog.Builder(this);
164 builder.setCancelable(true);
165 builder.setTitle(title);
166 builder.setMessage(message);
167 builder.show();
168 }
169
170 //clear the editext after saving
171 private void clear() {
172 editempname.setText("");
173 editempmail.setText("");
174 editempsalary.setText("");
175 }
176
177}