· 9 years ago · Dec 01, 2016, 10:38 AM
1int _id;
2String _name;
3String _mobile;
4String _home;
5String _office;
6String _email;
7String _companyName;
8String _jobRole;
9String _address;
10
11
12public Contacts( String name, String mobile, String home, String office, String address, String email,String companyName, String jobRole ) {
13
14 this._name = name;
15 this._mobile = mobile;
16 this._home = home;
17 this._office = office;
18 this._address = address;
19 this._email = email;
20 this._companyName = companyName;
21 this._jobRole = jobRole;
22
23}
24public Contacts(int id, String name, String mobile, String home, String office,String address,String email,String companyName, String jobRole ) {
25
26 this._id = id;
27 this._name = name;
28 this._mobile = mobile;
29 this._home = home;
30 this._office = office;
31 this._address = address;
32 this._email = email;
33 this._companyName = companyName;
34 this._jobRole = jobRole;
35
36}
37
38public Contacts() {
39
40}
41
42
43public String getCompanyName() {
44 return _companyName;
45}
46
47public void setCompanyName(String companyName) {
48 this._companyName = companyName;
49}
50
51public String getEmail() {
52 return _email;
53}
54
55public void setEmail(String email) {
56 this._email = email;
57}
58
59public String getHome() {
60 return _home;
61}
62
63public void setHome(String home) {
64 this._home = home;
65}
66
67public int getId() {
68 return _id;
69}
70
71public void setId(int id) {
72 this._id = id;
73}
74
75public String getJobRole() {
76 return _jobRole;
77}
78
79public void setJobRole(String jobRole) {
80 this._jobRole = jobRole;
81}
82
83public String getMobile() {
84 return _mobile;
85}
86
87public void setMobile(String mobile) {
88 this._mobile = mobile;
89}
90
91public String getName() {
92 return _name;
93}
94
95public void setName(String name) {
96 this._name = name;
97}
98
99public String getOffice() {
100 return _office;
101}
102
103public void setOffice(String office) {
104 this._office = office;
105}
106
107public String getAddress() {
108
109 return _address;
110}
111
112public void setAddress(String address) {
113 this._address = address;
114}
115
116DbHandler dbHandler;
117ListView listView;
118
119
120@Override
121protected void onCreate(@Nullable Bundle savedInstanceState) {
122 super.onCreate(savedInstanceState);
123 setContentView(R.layout.example4);
124
125
126
127 dbHandler = new DbHandler(this);
128 List<Contacts> listContacts = dbHandler.getAllContacts();
129 ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listContacts);
130
131 listView = (ListView) findViewById(R.id.listView1);
132 listView.setAdapter(arrayAdapter);
133 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
134 @Override
135 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
136
137 Bundle bundle = new Bundle();
138 int id_search = i ;
139 bundle.putInt("id",id_search);
140
141 Intent intent = new Intent(getApplicationContext(),DisplayContacts.class);
142 intent.putExtras(bundle);
143 startActivity(intent);
144
145 }
146 });
147
148
149
150
151
152
153 Log.d("Insert: ", "Inserting ..");
154 dbHandler.addContact(new Contacts("Ravi", "9100000000","044-123456","","","r@gmail.com","qbrik","developer"));
155 dbHandler.addContact(new Contacts("Srinivas", "9199999999","044-123456","","","r@gmail.com","qbrik","developer"));
156 dbHandler.addContact(new Contacts("Tommy", "9522222222","044-123456","","","r@gmail.com","qbrik","developer"));
157 dbHandler.addContact(new Contacts("Karthik", "9533333333","044-123456","","","r@gmail.com","qbrik","developer"));
158
159 Log.d("Reading: ", "Reading all contacts..");
160
161 for (Contacts cn : listContacts) {
162 String log = "Id: "+cn.getId()+" ,Name: " + cn.getName() + " ,Mobile: " + cn.getMobile() + " ,Home: " + cn.getHome() + " ,Office: " + cn.getOffice() + " ,Email: " + cn.getEmail() + " ,Company Name: " + cn.getCompanyName() + " ,Job Role: " + cn.getJobRole();
163 Log.d("Name: ", log);
164 }
165}
166@Override
167public boolean onCreateOptionsMenu(Menu menu) {
168 getMenuInflater().inflate(R.menu.menu_main, menu);
169 return true;
170}
171
172
173@Override
174public boolean onOptionsItemSelected(MenuItem item) {
175 super.onOptionsItemSelected(item);
176 switch(item.getItemId()) {
177 case R.id.item1:Bundle dataBundle = new Bundle();
178 dataBundle.putInt("id", 0);
179
180 Intent intent = new Intent(getApplicationContext(),DisplayContacts.class);
181 intent.putExtras(dataBundle);
182
183 startActivity(intent);
184 return true;
185 default:
186 return super.onOptionsItemSelected(item);
187 }
188
189}
190
191TextView name;
192TextView mobile;
193TextView home;
194TextView office;
195TextView companyName;
196TextView jobRole;
197TextView email;
198TextView address;
199private DbHandler dbHandler;
200private int id_update =0;
201private Contacts contacts;
202
203@Override
204protected void onCreate(Bundle savedInstanceState) {
205 super.onCreate(savedInstanceState);
206 setContentView(R.layout.example4_display);
207
208 name = (TextView) findViewById(R.id.editName);
209 mobile = (TextView) findViewById(R.id.editTextMobile);
210 home = (TextView) findViewById(R.id.editTextHome);
211 office = (TextView) findViewById(R.id.editTextOffice);
212 companyName = (TextView) findViewById(R.id.editTextComapanyName);
213 jobRole = (TextView) findViewById(R.id.editTextJobRole);
214 email = (TextView) findViewById(R.id.editEmail);
215 address = (TextView) findViewById(R.id.editAddress);
216
217 dbHandler = new DbHandler(this);
218 contacts = new Contacts();
219 Bundle bundle = getIntent().getExtras();
220 if(bundle != null) {
221
222 int value = bundle.getInt("id");
223 if(value > 0) {
224
225 Cursor cs = (Cursor) dbHandler.getContact(value);
226 id_update = value;
227 cs.moveToFirst();
228
229
230 String nam = cs.getString(cs.getColumnIndex(DbHandler.KEY_NAME));
231 String mob = cs.getString(cs.getColumnIndex(DbHandler.KEY_MOBILE));
232 String hom = cs.getString(cs.getColumnIndex(DbHandler.KEY_HOME));
233 String off = cs.getString(cs.getColumnIndex(DbHandler.KEY_OFFICE));
234 String com = cs.getString(cs.getColumnIndex(DbHandler.KEY_COMPANYNAME));
235 String job = cs.getString(cs.getColumnIndex(DbHandler.KEY_JOBROLE));
236 String emai = cs.getString(cs.getColumnIndex(DbHandler.KEY_EMAIL));
237 String addr = cs.getString(cs.getColumnIndex(DbHandler.KEY_ADDRESS));
238
239 if(!cs.isClosed()) {
240 cs.close();
241
242 Button b = (Button) findViewById(R.id.button1);
243 b.setVisibility(View.INVISIBLE);
244
245
246 name.setText(nam);
247 name.setFocusable(false);
248 name.setClickable(false);
249
250 mobile.setText(mob);
251 mobile.setFocusable(false);
252 mobile.setClickable(false);
253
254 home.setText(hom);
255 home.setFocusable(false);
256 home.setClickable(false);
257
258 office.setText(off);
259 office.setFocusable(false);
260 office.setClickable(false);
261
262 companyName.setText(com);
263 companyName.setFocusable(false);
264 companyName.setClickable(false);
265
266 jobRole.setText(job);
267 jobRole.setFocusable(false);
268 jobRole.setClickable(false);
269
270 email.setText(emai);
271 email.setFocusable(false);
272 email.setClickable(false);
273
274 address.setText(addr);
275 address.setFocusable(false);
276 address.setClickable(false);
277 }
278
279
280 }
281
282 }
283
284
285
286
287
288
289}
290
291public void run(View view) {
292
293 Bundle bundle = getIntent().getExtras();
294 if(bundle != null) {
295
296 int value =bundle.getInt("id");
297 if(value > 0) {
298 if(dbHandler.updateContact(new Contacts(id_update,name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {
299
300 Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
301 Intent intent = new Intent(getApplicationContext(),Example4.class);
302 startActivity(intent);
303 } else{
304 Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
305 }
306 } else{
307 if(dbHandler.addContact(new Contacts(name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {
308 Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
309 } else{
310 Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
311 }
312 Intent intent = new Intent(getApplicationContext(),Example4.class);
313 startActivity(intent);
314 }
315 }
316
317
318}
319
320@Override
321public boolean onCreateOptionsMenu(Menu menu) {
322
323 Bundle bundle = getIntent().getExtras();
324 if(bundle !=null) {
325 int value = bundle.getInt("id");
326 if(value > 0) {
327 getMenuInflater().inflate(R.menu.display_contact,menu);
328 }else {
329 getMenuInflater().inflate(R.menu.menu_main, menu);
330 }
331 }
332
333 return true;
334}
335
336@Override
337public boolean onOptionsItemSelected(MenuItem item) {
338 super.onOptionsItemSelected(item);
339
340 switch(item.getItemId()) {
341 case R.id.Edit_Contact:
342
343 Button b = (Button) findViewById(R.id.button1);
344 b.setVisibility(View.VISIBLE);
345
346 name.setEnabled(true);
347 name.setFocusableInTouchMode(true);
348 name.setClickable(true);
349
350 mobile.setEnabled(true);
351 mobile.setFocusableInTouchMode(true);
352 mobile.setClickable(true);
353
354 home.setEnabled(true);
355 home.setFocusableInTouchMode(true);
356 home.setClickable(true);
357
358 office.setEnabled(true);
359 office.setFocusableInTouchMode(true);
360 office.setClickable(true);
361
362 companyName.setEnabled(true);
363 companyName.setFocusableInTouchMode(true);
364 companyName.setClickable(true);
365
366 jobRole.setEnabled(true);
367 jobRole.setFocusableInTouchMode(true);
368 jobRole.setClickable(true);
369
370 email.setEnabled(true);
371 email.setFocusableInTouchMode(true);
372 email.setClickable(true);
373
374 address.setEnabled(true);
375 address.setFocusableInTouchMode(true);
376 address.setClickable(true);
377
378 return true;
379 case R.id.Delete_Contact:
380
381 return true;
382 default:
383 return super.onOptionsItemSelected(item);
384 }
385
386
387}
388
389public static final String DATABASE_NAME = "contact"; //database name
390public static final int DATABASE_VERSION = 1; //database version
391public static final String TABLE_CONTACTS = "contacts";//table name
392public static final String KEY_ID = "id"; //column names for contacts table
393public static final String KEY_NAME = "name";
394public static final String KEY_MOBILE = "mobile";
395public static final String KEY_HOME = "home";
396public static final String KEY_OFFICE = "office";
397public static final String KEY_ADDRESS = "address";
398public static final String KEY_EMAIL = "email";
399public static final String KEY_COMPANYNAME = "companyName";
400public static final String KEY_JOBROLE = "jobRole";
401
402
403
404public DbHandler(Context context) {
405 super(context, DATABASE_NAME, null, DATABASE_VERSION);
406}
407
408
409
410//to create taables
411@Override
412public void onCreate(SQLiteDatabase sqLiteDatabase) {
413
414 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
415 + KEY_NAME + " TEXT,"
416 + KEY_MOBILE + " TEXT,"
417 + KEY_HOME + " TEXT,"
418 + KEY_OFFICE + " TEXT,"
419 + KEY_ADDRESS + " TEXT,"
420 + KEY_EMAIL + " TEXT,"
421 + KEY_COMPANYNAME + " TEXT,"
422 + KEY_JOBROLE + " TEXT" + ")";
423 sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);
424
425}
426//upgrading database i.e., if further we add any columns to the existing table ,it will modify automatically
427@Override
428public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
429
430 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_CONTACTS); //drop older table if exists
431 onCreate(sqLiteDatabase); //to create table again
432
433}
434
435public boolean addContact(Contacts contacts) {
436
437 SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
438 ContentValues contentValues = new ContentValues();
439 contentValues.put(KEY_NAME,contacts.getName());
440 contentValues.put(KEY_MOBILE,contacts.getMobile());
441 contentValues.put(KEY_HOME,contacts.getHome());
442 contentValues.put(KEY_OFFICE,contacts.getOffice());
443 contentValues.put(KEY_ADDRESS,contacts.getAddress());
444 contentValues.put(KEY_EMAIL,contacts.getEmail());
445 contentValues.put(KEY_COMPANYNAME,contacts.getCompanyName());
446 contentValues.put(KEY_JOBROLE,contacts.getJobRole());
447
448
449 sqLiteDatabase.insert(TABLE_CONTACTS,null,contentValues);
450 sqLiteDatabase.close();
451 return true;
452}
453
454
455public Contacts getContact(int id) {
456
457 SQLiteDatabase sqLiteDatabase =this.getReadableDatabase();
458 Cursor cursor = sqLiteDatabase.query(TABLE_CONTACTS,new String[] {KEY_ID,KEY_NAME,KEY_MOBILE,KEY_HOME,KEY_OFFICE,KEY_ADDRESS,KEY_EMAIL,KEY_COMPANYNAME,KEY_JOBROLE},KEY_ID + "=?",new String[] {String.valueOf(id)},null,null,null,null);
459 if(cursor != null)
460 cursor.moveToFirst();
461 Contacts contacts = new Contacts(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));
462
463 return contacts;
464
465
466}
467
468public List<Contacts>getAllContacts() {
469
470 List<Contacts> contactsList = new ArrayList<Contacts>();
471 String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
472 SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
473 Cursor cursor = sqLiteDatabase.rawQuery(selectQuery,null);
474
475 if (cursor.moveToFirst()){
476
477 do {
478
479 Contacts contacts = new Contacts();
480 contacts.setId(Integer.parseInt(cursor.getString(0)));
481 contacts.setName(cursor.getString(1));
482 contacts.setMobile(cursor.getString(2));
483 contacts.setHome(cursor.getString(3));
484 contacts.setOffice(cursor.getString(4));
485 contacts.setAddress(cursor.getString(5));
486 contacts.setEmail(cursor.getString(6));
487 contacts.setCompanyName(cursor.getString(7));
488 contacts.setJobRole(cursor.getString(8));
489
490 contactsList.add(contacts);
491
492
493 }while (cursor.moveToNext());
494
495 }
496 return contactsList;
497
498}
499
500public int getContactsCount() {
501
502 String countQuery = " SELECT * FROM " + TABLE_CONTACTS;
503 SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
504 Cursor cursor = sqLiteDatabase.rawQuery(countQuery,null);
505 cursor.close();
506
507 return cursor.getCount();
508
509}
510
511public boolean updateContact(Contacts contacts) {
512
513 SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
514 ContentValues contentValues = new ContentValues();
515 contentValues.put(KEY_NAME,contacts.getName());
516 contentValues.put(KEY_MOBILE,contacts.getMobile());
517 contentValues.put(KEY_HOME,contacts.getHome());
518 contentValues.put(KEY_OFFICE,contacts.getOffice());
519 contentValues.put(KEY_ADDRESS,contacts.getAddress());
520 contentValues.put(KEY_EMAIL,contacts.getEmail());
521 contentValues.put(KEY_COMPANYNAME,contacts.getCompanyName());
522 contentValues.put(KEY_JOBROLE,contacts.getJobRole());
523 sqLiteDatabase.update(TABLE_CONTACTS,contentValues,KEY_ID + "= ?",new String[] {String.valueOf(contacts.getId())});
524 return true;
525
526
527}
528
529public void deleteContact(Contacts contacts) {
530
531 SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
532 sqLiteDatabase.delete(TABLE_CONTACTS,KEY_ID + "= ?",new String[]{String.valueOf(contacts.getId())});
533 sqLiteDatabase.close();
534
535}
536
537if(dbHandler.updateContact(new Contacts(id_update,name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {}
538
539public Boolean addContact(Contacts contacts) {
540
541 SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
542 ContentValues contentValues = new ContentValues();
543 contentValues.put(KEY_NAME,contacts.getName());
544 contentValues.put(KEY_MOBILE,contacts.getMobile());
545 contentValues.put(KEY_HOME,contacts.getHome());
546 contentValues.put(KEY_OFFICE,contacts.getOffice());
547 contentValues.put(KEY_ADDRESS,contacts.getAddress());
548 contentValues.put(KEY_EMAIL,contacts.getEmail());
549 contentValues.put(KEY_COMPANYNAME,contacts.getCompanyName());
550 contentValues.put(KEY_JOBROLE,contacts.getJobRole());
551
552
553 sqLiteDatabase.insert(TABLE_CONTACTS,null,contentValues);
554 sqLiteDatabase.close();
555
556 return true;
557 }