· 5 years ago · Aug 29, 2020, 09:22 AM
1package androidtutorial.project.admissionmgtsystem;
2
3import androidx.appcompat.app.AppCompatActivity;
4
5import android.app.DatePickerDialog;
6import android.content.Intent;
7import android.database.sqlite.SQLiteDatabase;
8import android.graphics.Color;
9import android.os.Bundle;
10import android.view.View;
11import android.widget.Button;
12import android.widget.DatePicker;
13import android.widget.EditText;
14import android.widget.Spinner;
15import android.widget.TextView;
16import android.widget.Toast;
17
18import com.google.android.material.textfield.TextInputLayout;
19
20import java.text.SimpleDateFormat;
21import java.util.ArrayList;
22import java.util.Calendar;
23import java.util.Locale;
24
25import cn.pedant.SweetAlert.SweetAlertDialog;
26
27public class StudentRegistered extends AppCompatActivity implements View.OnClickListener {
28 EditText editTextName, editTextRollNo, editTextCDate, editTextPassword, editTextSection;
29 TextView textViewClickhere;
30 Button buttonRegistered;
31 Spinner spinnerClass;
32 StudentData studentData;
33 DataBase dataBase;
34
35 @Override
36 protected void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
38 setContentView(R.layout.activity_student_registered);
39 // xml resource initialize
40 editTextName = (EditText) findViewById(R.id.nameId);
41 editTextRollNo = (EditText) findViewById(R.id.rollNoId);
42 editTextCDate = (EditText) findViewById(R.id.cDateId);
43 editTextPassword = (EditText) findViewById(R.id.passwordId);
44 editTextSection = (EditText) findViewById(R.id.classSectionId);
45 textViewClickhere = (TextView) findViewById(R.id.clickHereId);
46 buttonRegistered = (Button) findViewById(R.id.registeredBtnId);
47 spinnerClass = (Spinner) findViewById(R.id.classSpinner);
48 dataBase = new DataBase(this);
49 studentData=new StudentData();
50 buttonRegistered.setOnClickListener(this);
51 textViewClickhere.setOnClickListener(this);
52
53
54 }
55
56 @Override
57 public void onClick(View v) {
58 switch (v.getId()) {
59 case R.id.registeredBtnId:
60 studentRegistered();
61 if (dataBase.insertQuery()) {
62 Toast.makeText(this, "Registered Successfully", Toast.LENGTH_SHORT).show();
63 Intent intent = new Intent(this, StudentHome.class);
64 startActivity(intent);
65 finish();
66 } else {
67 SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE);
68 pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
69 pDialog.setTitleText("Oops...");
70 pDialog.setContentText("User already exist.");
71 pDialog.show();
72 }
73 break;
74 case R.id.clickHereId:
75 Intent intentToLoginAc = new Intent(this, StudentLogin.class);
76 startActivity(intentToLoginAc);
77 break;
78 default:
79 }
80 }
81
82 private void studentRegistered() {
83 String stdName = editTextName.getText().toString().trim();
84 String stdRollNo = editTextRollNo.getText().toString().trim();
85 String stdClass = spinnerClass.getSelectedItem().toString().trim();
86 String stdClassSection = editTextSection.getText().toString().trim();
87 String stdPassword = editTextPassword.getText().toString().trim();
88 final Calendar myCalendar = Calendar.getInstance();
89 DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
90 @Override
91 public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
92 myCalendar.set(Calendar.YEAR, year);
93 myCalendar.set(Calendar.MONTH, month);
94 myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
95 }
96 };
97 String myFormat = "MM/dd/yy"; //In which you need put here
98 SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
99 editTextCDate.setText(sdf.format(myCalendar.getTime()));
100 String cDate = sdf.format(myCalendar.getTime());
101 if (stdName.isEmpty()) {
102 editTextName.setFocusable(false);
103 editTextName.setError("Name is required");
104 } else if (stdRollNo.isEmpty()) {
105 editTextRollNo.setFocusable(false);
106 editTextRollNo.setError("Roll No is required");
107 } else if (stdPassword.isEmpty()) {
108 editTextPassword.setFocusable(false);
109 editTextPassword.setError("Password is required");
110 } else if (cDate.isEmpty()) {
111 editTextCDate.setFocusable(false);
112 editTextCDate.setError("CDate is required");
113 } else {
114 // here i set value
115 studentData.setStdName(stdName);
116 studentData.setStdRollNo(stdRollNo);
117 studentData.setStdClass(stdClass);
118 studentData.setStdSection(stdClassSection);
119 studentData.setStdAcCDate(cDate);
120 studentData.setStdPassword(stdPassword);
121 }
122 }
123}
124// getter and setter define here
125package androidtutorial.project.admissionmgtsystem;
126
127public class StudentData {
128 public String stdName, stdRollNo, stdClass, stdSection, stdPassword, stdAcCDate;
129 public StudentData(){}
130 /*public StudentData(String stdName, String stdRollNo, String stdClass, String stdSection, String stdPassword, String stdAcCDate) {
131 this.stdName = stdName;
132 this.stdRollNo = stdRollNo;
133 this.stdClass = stdClass;
134 this.stdSection = stdSection;
135 this.stdPassword = stdPassword;
136 this.stdAcCDate = stdAcCDate;
137 }*/
138
139 public String getStdName() {
140 return stdName;
141 }
142
143 public void setStdName(String stdName) {
144 this.stdName = stdName;
145 }
146
147 public String getStdRollNo() {
148 return stdRollNo;
149 }
150
151 public void setStdRollNo(String stdRollNo) {
152 this.stdRollNo = stdRollNo;
153 }
154
155 public String getStdClass() {
156 return stdClass;
157 }
158
159 public void setStdClass(String stdClass) {
160 this.stdClass = stdClass;
161 }
162
163 public String getStdSection() {
164 return stdSection;
165 }
166
167 public void setStdSection(String stdSection) {
168 this.stdSection = stdSection;
169 }
170
171 public String getStdPassword() {
172 return stdPassword;
173 }
174
175 public void setStdPassword(String stdPassword) {
176 this.stdPassword = stdPassword;
177 }
178
179 public String getStdAcCDate() {
180 return stdAcCDate;
181 }
182
183 public void setStdAcCDate(String stdAcCDate) {
184 this.stdAcCDate = stdAcCDate;
185 }
186}
187// Error int class where i get the null value of studentData.getStdRollNo() while i set the value
188package androidtutorial.project.admissionmgtsystem;
189
190import android.content.ContentValues;
191import android.content.Context;
192import android.database.Cursor;
193import android.database.sqlite.SQLiteDatabase;
194import android.database.sqlite.SQLiteOpenHelper;
195
196public class DataBase extends SQLiteOpenHelper {
197 private static final int DATABASE_VERSION = 1;
198 private static final String DATABASE_NAME = "AttendanceMgtSystem";
199 private static final String StudentAC_TB = "Student_TB";
200 private static final String AdminAC_TB = "Admin_TB";
201 private static final String Attendance_TB = "Attendance_TB";
202 //create column name for student table
203 private static final String studentId = "studentId";
204 private static final String studentRollNo = "studentRollNo";
205 private static final String studentName = "studentName";
206 private static final String studentClass = "className";
207 private static final String classSection = "classSection";
208 private static final String studentPicture = "studentPicture";
209 private static final String studentAcDate = "createdDate";
210 private static final String studentPassword = "studentPassword";
211 // create column name for admin table
212 private static final String adminId = "adminId";
213 private static final String adminName = "adminName";
214 private static final String adminEmail = "adminEmail";
215 private static final String adminPicture = "adminPicture";
216 private static final String adminPassword = "adminPassword";
217 private static final String adminAcDate = "adminCDate";
218 // created column name for attendance table
219 private static final String attendanceId = "attendanceId";
220 private static final String attendanceStatus = "attendanceStatus";
221 private static final String attendanceDate = "attendanceDate";
222 StudentData studentData;
223 public DataBase(Context context) {
224 super(context, DATABASE_NAME, null, DATABASE_VERSION);
225 studentData=new StudentData();
226 }
227
228 @Override
229 public void onCreate(SQLiteDatabase db) {
230 // create table of student
231 String createStudentTB = "Create table " + StudentAC_TB + "(" + studentId + " INTEGER PRIMARY KEY AUTOINCREMENT," +
232 "" + studentRollNo + " TEXT NOT NULL," + studentName + " TEXT NOT NULL," + studentClass + " TEXT NOT NULL," +
233 "" + classSection + " TEXT," + studentPicture + " LONGBLOB," +
234 "" + studentAcDate + " DATE NOT NULL," + studentPassword + " TEXT NOT NULL" + ")";
235 // create table of admin
236 String createAdminTB = "Create table " + AdminAC_TB + "(" + adminId + " INTEGER PRIMARY KEY AUTOINCREMENT," +
237 "" + adminName + " TEXT NOT NULL," + adminEmail + " TEXT NOT NULL," + adminAcDate + " DATE NOT NULL," +
238 "" + adminPassword + " TEXT NOT NULL," + adminPicture + " LONGBLOB" + ")";
239 // create table of attendance
240 String createAttendanceTB = "Create table " + Attendance_TB + "(" + attendanceId + " INTEGER PRIMARY KEY AUTOINCREMENT," +
241 "" + attendanceStatus + " INTEGER NOT NULL," + attendanceDate + " DATE NOT NULL," +studentId+" INTEGER,"+adminId+" INTEGER,"+
242 "FOREIGN KEY (" + studentId + ") REFERENCES " + StudentAC_TB + "(" + studentId + ")," +
243 "FOREIGN KEY (" + adminId + ") REFERENCES " + AdminAC_TB + "(" + adminId + ")" + ")";
244 // execute queries for all table
245 try {
246 db.execSQL(createStudentTB);
247 db.execSQL(createAdminTB);
248 db.execSQL(createAttendanceTB);
249 } catch (Exception e) {
250 e.getMessage();
251 }
252
253 }
254
255 @Override
256 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
257 db.execSQL("DROP TABLE IF EXISTS " + StudentAC_TB);
258 db.execSQL("DROP TABLE IF EXISTS " + AdminAC_TB);
259 db.execSQL("DROP TABLE IF EXISTS " + Attendance_TB);
260
261 // Create tables again
262 onCreate(db);
263 }
264
265 public boolean insertQuery() {
266 SQLiteDatabase db = this.getWritableDatabase();
267 if (selectQuery()) {
268 ContentValues contentValues = new ContentValues();
269 contentValues.put(studentName, studentData.getStdName());
270 contentValues.put(studentRollNo, studentData.getStdRollNo());
271 contentValues.put(studentClass, studentData.getStdClass());
272 contentValues.put(classSection, studentData.getStdSection());
273 contentValues.put(studentAcDate, studentData.getStdAcCDate());
274 contentValues.put(studentPassword, studentData.getStdPassword());
275 db.insert(StudentAC_TB, null, contentValues);
276 db.close();
277 return true;
278 } else {
279 return false;
280 }
281
282 }
283
284 public boolean selectQuery() {
285 SQLiteDatabase db = this.getWritableDatabase();
286 try{
287 String checkStudent = "Select * from "+StudentAC_TB + " where "+studentRollNo+"=" + studentData.getStdRollNo();//error here
288 Cursor cursor = db.rawQuery(checkStudent, null);
289 if (cursor.getCount() <= 0) {
290 cursor.close();
291 return true;
292 } else {
293 cursor.close();
294 return false;
295 }
296 }catch (Exception e){
297 e.getMessage();
298 }
299 return false;
300 }
301
302 public void updateQuery() {
303
304 }
305}
306