· 7 years ago · Oct 14, 2018, 07:26 PM
1import android.content.ContentValues;
2import android.content.Context;
3import android.database.Cursor;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6
7import customandroidtoastdemo.app.com.mad_final_end.UserProfile;
8
9import static android.os.Build.ID;
10import static customandroidtoastdemo.app.com.mad_final_end.UserProfile.Users.COL_1;
11import static customandroidtoastdemo.app.com.mad_final_end.UserProfile.Users.COL_2;
12import static customandroidtoastdemo.app.com.mad_final_end.UserProfile.Users.COL_3;
13import static customandroidtoastdemo.app.com.mad_final_end.UserProfile.Users.COL_4;
14import static customandroidtoastdemo.app.com.mad_final_end.UserProfile.Users.COL_5;
15
16/**
17 * Created by Kalana on 10/13/2018.
18 */
19
20public class DBHelper extends SQLiteOpenHelper {
21
22 private static final String DATABASE_NAME="Users.db";
23
24 public DBHelper(Context context) {
25 super(context, DATABASE_NAME, null, 1);
26 }
27
28 @Override
29 public void onCreate(SQLiteDatabase db) {
30
31 db.execSQL(" CREATE TABLE " +
32 UserProfile.Users.TABLE_NAME+" ("+
33 COL_1+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
34 UserProfile.Users.COL_2+" TEXT, " +
35 UserProfile.Users.COL_3+" TEXT, " +
36 UserProfile.Users.COL_4+" TEXT, "+
37 UserProfile.Users.COL_5+" TEXT ) ") ;
38 }
39
40 @Override
41 public void onUpgrade(SQLiteDatabase db, int i, int i1) {
42
43 db.execSQL(" DROP TABLE IF EXISTS " + UserProfile.Users.TABLE_NAME);
44 onCreate(db);
45
46 }
47
48//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
49 public boolean AddInfo(String uName, String dOfBth, String password, String gender){
50
51 SQLiteDatabase db = this.getWritableDatabase();
52
53 ContentValues contentValues=new ContentValues();
54 contentValues.put(UserProfile.Users.COL_2,uName);
55 contentValues.put(UserProfile.Users.COL_3,dOfBth);
56 contentValues.put(UserProfile.Users.COL_4,password);
57 contentValues.put(UserProfile.Users.COL_5,gender);
58
59 try{
60 long result= db.insert(UserProfile.Users.TABLE_NAME,null,contentValues);
61 return true;
62 }
63 catch(Exception e){
64 e.printStackTrace();
65 return false;
66 }
67
68
69
70
71 }
72
73 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>..
74 public Cursor readAllData(){
75
76 SQLiteDatabase db = this.getWritableDatabase();
77 Cursor res =db.rawQuery(" select * from "+UserProfile.Users.TABLE_NAME,null);
78 return res;
79
80
81 }
82
83 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
84 public Cursor readSingaleData(String id){
85
86 SQLiteDatabase db = this.getWritableDatabase();
87 Cursor res =db.rawQuery(" select * from "+UserProfile.Users.TABLE_NAME+ " where "+UserProfile.Users.COL_1 + " = '" +id+ "' ",null);
88 return res;
89
90
91 }
92
93//>>>>>>>>>>>
94 public Integer deleteInfo(String id){
95
96 SQLiteDatabase db= this.getWritableDatabase();
97
98 return db.delete(UserProfile.Users.TABLE_NAME,"id=?",new String[]{id});
99
100
101 }
102
103
104 public boolean update(String id,String username,String DofBth,String password,String gender){
105
106 SQLiteDatabase db=this.getWritableDatabase();
107
108 ContentValues contentValues= new ContentValues();
109 contentValues.put(COL_1,id);
110 contentValues.put(COL_2,username);
111 contentValues.put(COL_3,DofBth);
112 contentValues.put(COL_4,password);
113 contentValues.put(COL_5,gender);
114
115 db.update(UserProfile.Users.TABLE_NAME,contentValues,"id= ?",new String[] {id});
116
117 return true;
118
119
120
121 }
122
123
124
125
126
127
128
129
130
131}
132
133import android.content.Intent;
134import android.support.v7.app.AppCompatActivity;
135import android.os.Bundle;
136import android.view.View;
137import android.widget.Button;
138
139public class Home extends AppCompatActivity {
140
141
142
143 Button Reg ;
144
145 @Override
146 protected void onCreate(Bundle savedInstanceState) {
147 super.onCreate(savedInstanceState);
148 setContentView(R.layout.activity_home);
149
150 Reg=(Button)findViewById(R.id.button_RegisterHome);
151 Reg.setOnClickListener(new View.OnClickListener() {
152 @Override
153 public void onClick(View view) {
154
155 Intent startIntent = new Intent(getApplicationContext(), ProfileManagement.class);
156 startActivity(startIntent);
157
158
159 }
160 });
161
162
163
164 }
165
166
167
168}
169
170import android.database.Cursor;
171import android.support.v7.app.AppCompatActivity;
172import android.os.Bundle;
173import android.view.View;
174import android.widget.Button;
175import android.widget.EditText;
176import android.widget.RadioButton;
177import android.widget.RadioGroup;
178import android.widget.Toast;
179
180import customandroidtoastdemo.app.com.mad_final_end.Database.DBHelper;
181
182public class EditProfile extends AppCompatActivity {
183
184
185 Button deletebtn;
186 Button update;
187 Button search;
188 DBHelper myDB;
189 EditText username,DofBth,password,id;
190 RadioGroup RdioGrp;
191 RadioButton Rdbtn;
192
193
194
195 @Override
196 protected void onCreate(Bundle savedInstanceState) {
197 super.onCreate(savedInstanceState);
198 setContentView(R.layout.activity_edit_profile);
199
200 myDB = new DBHelper(this);
201 id =(EditText)findViewById(R.id.editText_ID);
202 username =(EditText)findViewById(R.id.editText_UserNameEdit);
203 DofBth =(EditText)findViewById(R.id.editText_DofBth);
204 password =(EditText)findViewById(R.id.editText_PasswordEdit);
205 RdioGrp =(RadioGroup) findViewById(R.id.RdioGrp);
206 deletebtn =(Button)findViewById(R.id.button_DeletePro);
207 update =(Button)findViewById(R.id.button_EditPro);
208 search =(Button)findViewById(R.id.button_SearchEdit);
209
210
211 delete();
212 update();
213 readSingaleInfo();
214
215
216
217 }
218
219
220 public void delete(){
221
222 deletebtn.setOnClickListener(new View.OnClickListener() {
223 @Override
224 public void onClick(View view) {
225
226 Integer deletedRows = myDB.deleteInfo(id.getText().toString());
227
228 if(deletedRows>0){
229
230 Toast.makeText(EditProfile.this, "Data Is deleted", Toast.LENGTH_SHORT).show();
231 }
232 else{
233
234 Toast.makeText(EditProfile.this,"Not deleted",Toast.LENGTH_SHORT).show();
235 }
236
237
238
239 }
240 });
241
242
243
244 }
245
246
247
248 public void update(){
249
250 update.setOnClickListener(new View.OnClickListener() {
251 @Override
252 public void onClick(View view) {
253
254 int SelectedID1= RdioGrp.getCheckedRadioButtonId();
255 Rdbtn=(RadioButton)findViewById(SelectedID1);
256
257 boolean isUpdated=myDB.update(id.getText().toString(),username.getText().toString(),DofBth.getText().toString(),password.getText().toString(),Rdbtn.getText().toString());
258 if(isUpdated =true){
259
260 Toast.makeText(EditProfile.this,"Data is Updated",Toast.LENGTH_SHORT).show();
261
262 }
263 else{
264
265 Toast.makeText(EditProfile.this,"Data is not Updated",Toast.LENGTH_SHORT).show();
266 }
267
268
269
270
271 }
272 });
273
274
275
276 }
277
278
279 public void readSingaleInfo( ){
280 search.setOnClickListener(
281
282 new View.OnClickListener() {
283 @Override
284 public void onClick(View view) {
285
286 Cursor res= myDB.readSingaleData(id.getText().toString());
287 if(res.getCount()==0){
288
289 //message
290 showmessage("Error","Nothing Found from System");
291 return;
292
293 }
294 else{
295 StringBuffer buffer= new StringBuffer();
296
297 // we used while lopp to check all rows becasse we dont knw how many rows are there
298 while(res.moveToNext()){
299
300 buffer.append("id :"+ res.getString(0)+ "n");
301 buffer.append("username :"+ res.getString(1)+ "n");
302 buffer.append("DofBirth :"+ res.getString(2)+ "n");
303 // buffer.append("Password :"+ res.getString(3)+ "n");
304 buffer.append("Gender :"+ res.getString(4)+ "n");
305
306
307
308 }
309
310 showmessage("Data",buffer.toString());
311
312 }
313 }
314 }
315 );
316
317
318 }
319
320 public void showmessage(String title, String Message){
321 android.support.v7.app.AlertDialog.Builder builder= new android.support.v7.app.AlertDialog.Builder(this);
322 builder.setCancelable(true);
323 builder.setTitle(title);
324 builder.setMessage(Message);
325 builder.show();
326
327
328 }
329
330
331}
332
333import android.content.Intent;
334import android.database.Cursor;
335import android.support.v7.app.AppCompatActivity;
336import android.os.Bundle;
337import android.view.View;
338import android.widget.Button;
339import android.widget.EditText;
340import android.widget.RadioButton;
341import android.widget.RadioGroup;
342import android.widget.Toast;
343
344import customandroidtoastdemo.app.com.mad_final_end.Database.DBHelper;
345
346public class ProfileManagement extends AppCompatActivity {
347
348 Button Add;
349 Button View,Edit;
350 EditText username,DofBth,password,id;
351 RadioGroup RdioGrp;
352 RadioButton Rdbtn;
353
354
355 DBHelper myDB;
356
357
358
359
360 @Override
361 protected void onCreate(Bundle savedInstanceState) {
362 super.onCreate(savedInstanceState);
363 setContentView(R.layout.activity_profile_management);
364
365 myDB = new DBHelper(this);
366
367 username=(EditText)findViewById(R.id.editText_usernamePro);
368 DofBth =(EditText)findViewById(R.id.editText_dOfBthPro);
369 password=(EditText)findViewById(R.id.editText_passwordPro);
370 RdioGrp =(RadioGroup)findViewById(R.id.RdGrp);
371 Add = (Button)findViewById(R.id.button_addPro);
372 View=(Button)findViewById(R.id.button_ViewPro);
373
374 Edit=(Button)findViewById(R.id.button_EditPro);
375 Edit.setOnClickListener(new View.OnClickListener() {
376 @Override
377 public void onClick(View view) {
378
379 Intent startIntent = new Intent(getApplicationContext(), EditProfile.class);
380 startActivity(startIntent);
381
382
383 }
384 });
385
386
387
388 AddInfo();
389 readAllInfo();
390
391
392
393
394
395 }
396
397 public void AddInfo(){
398
399 Add.setOnClickListener(new View.OnClickListener() {
400 @Override
401 public void onClick(View view) {
402
403 int SelectedID= RdioGrp.getCheckedRadioButtonId();//pass the radio button value to the vairible
404 Rdbtn=(RadioButton)findViewById(SelectedID);
405
406
407 boolean isInserted = myDB.AddInfo(username.getText().toString(),DofBth.getText().toString(),password.getText().toString(),Rdbtn.getText().toString());
408
409 if(isInserted=true){
410
411 Toast.makeText(ProfileManagement.this, "Data Is Inserted", Toast.LENGTH_SHORT).show();
412 }
413 else{
414 Toast.makeText(ProfileManagement.this,"Not Inserted",Toast.LENGTH_SHORT).show();
415 }
416
417
418 }
419 });
420
421 }
422
423
424 public void readAllInfo(){
425 View.setOnClickListener(
426
427 new View.OnClickListener() {
428 @Override
429 public void onClick(View view) {
430
431 Cursor res= myDB.readAllData();
432 if(res.getCount()==0){
433
434 //message
435 showmessage("Error","Nothing Found from System");
436 return;
437
438 }
439 else{
440 StringBuffer buffer= new StringBuffer();
441
442 // we used while lopp to check all rows becasse we dont knw how many rows are there
443 while(res.moveToNext()){
444
445 buffer.append("id :"+ res.getString(0)+ "n");
446 buffer.append("username :"+ res.getString(1)+ "n");
447 buffer.append("DofBirth :"+ res.getString(2)+ "n");
448 // buffer.append("Password :"+ res.getString(3)+ "n");
449 buffer.append("Gender :"+ res.getString(4)+ "n");
450
451
452
453 }
454
455 showmessage("Data",buffer.toString());
456
457 }
458 }
459 }
460 );
461
462
463 }
464
465
466 public void showmessage(String title, String Message){
467 android.support.v7.app.AlertDialog.Builder builder= new android.support.v7.app.AlertDialog.Builder(this);
468 builder.setCancelable(true);
469 builder.setTitle(title);
470 builder.setMessage(Message);
471 builder.show();
472
473
474 }
475
476
477
478
479
480}
481
482import android.provider.BaseColumns;
483
484/**
485 * Created by Kalana on 10/13/2018.
486 */
487
488public final class UserProfile {
489
490 //default Constructer
491 private UserProfile(){}
492
493 //Inner Class
494 public class Users implements BaseColumns{
495
496
497 public static final String TABLE_NAME="UserInfo";
498 public static final String COL_1="id";
499 public static final String COL_2="username";
500 public static final String COL_3="dOfBth";
501 public static final String COL_4="password";
502 public static final String COL_5="gender";
503
504
505
506
507
508 }
509
510}