· 8 years ago · Feb 07, 2018, 03:38 AM
1import android.content.ContentValues;
2import android.content.Context;
3import android.database.Cursor;
4import android.database.SQLException;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7import android.util.Log;
8import android.widget.Toast;
9
10public class DataBaseSampleActivity {
11
12 /** for database */
13 static final String DataBaseName = "EmployeDB";
14
15 /** for employee table */
16 static final String EmployeTable = "Employees";
17 static final String ColEmpID = "EmpId";
18 static final String ColEmpName = "EmpName";
19 static final String ColEmpAge = "EmpAge";
20 static final String ColDept = "Dept";
21
22 /** for department table */
23 static final String DeptTable = "Department";
24 static final String ColDeptID = "DeptId";
25 static final String ColDeptName = "DeptName";
26
27 public static final int DATABASE_VERSION = 2;
28
29 //private static final String KEY_ROWID = "_id";
30
31 private static final String EMPLOYEE_TABLE_CREATE ="Create table " + EmployeTable +
32 //"(_id INTEGER UNIQUE," + [old code]
33 "("+ColEmpID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
34 ColEmpName + " VARCHAR(15) ," +
35 ColEmpAge + " INT(15) ," +
36 ColDept + " VARCHAR(15)) ";
37
38 private final Context context;
39 private DatabaseHelper DBHelper;
40 private SQLiteDatabase db;
41
42 public DataBaseSampleActivity(Context ctx){
43 Log.i("test****", "**test***");
44 this.context = ctx;
45 DBHelper = new DatabaseHelper(context);
46 }
47 private static class DatabaseHelper extends SQLiteOpenHelper{
48 public DatabaseHelper(Context context){
49 super(context, DataBaseName , null, DATABASE_VERSION);
50 Log.i("context","context");
51 }
52
53 @Override
54 public void onCreate(SQLiteDatabase db) {
55 // TODO Auto-generated method stub
56 db.execSQL(EMPLOYEE_TABLE_CREATE);
57 Log.i("************", "table created");
58 }
59
60 @Override
61 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
62 // TODO Auto-generated method stub
63 Log.w("tag", "Upgrading database from version " + oldVersion + " to "+ newVersion + ", which will destroy all old data");
64 db.execSQL("DROP TABLE IF EXISTS " + EmployeTable);
65 onCreate(db);
66 }
67
68 };
69
70 public DataBaseSampleActivity open() throws SQLException{
71 db = DBHelper.getWritableDatabase();
72 Log.i("open", "message");
73 return this;
74 }
75 public void close(){
76 DBHelper.close();
77 }
78
79 //public long insert(Integer empid, String empname, Integer empage, String empdept) {
80 public long insert(String empname, Integer empage, String empdept) {
81 Log.i("**** suruchitest **** ","*** test ***");
82 ContentValues initialValues = new ContentValues();
83
84 //initialValues.put(ColEmpID, empid);
85 initialValues.put(ColEmpName, empname);
86 initialValues.put(ColEmpAge, empage);
87 initialValues.put(ColDept, empdept);
88
89 return db.insert(EmployeTable, null, initialValues);
90 }
91
92 public Cursor getEmpValues(){
93 Cursor mCursor = db.query(EmployeTable, null, null, null, null, null, null);
94 return mCursor;
95 }
96
97 public boolean deleteEmpList(long rowId){
98 Toast.makeText(context, "deleted", 2000).show();
99 return db.delete(EmployeTable, ColEmpID +" = " + rowId, null) > 0;
100 }
101 public boolean updateEmplist(String empname, Integer empage, String empdept, Integer rowid){
102
103 ContentValues initialValues = new ContentValues();
104 Log.i("##### "+rowid,""+empname+" "+empage+" "+empdept);
105
106 //initialValues.put(ColEmpID, rowid);
107 initialValues.put(ColEmpName,empname);
108 initialValues.put(ColEmpAge,empage);
109 initialValues.put(ColDept,empdept);
110
111 try{
112 int b = db.update(EmployeTable, initialValues, ColEmpID+ " = " + rowid, null);
113 Log.i("update", "up "+rowid+" ddd "+b);
114 return true;
115 }catch (Exception e){
116 Log.d("asdfasdfsadfasdf", "_--___--__--_=-_");
117 return false;
118 }
119 }
120}
121
122
123
124
1252. create Main Activity
126
127
128package com.db;
129
130import android.app.Activity;
131import android.content.Intent;
132import android.os.Bundle;
133import android.util.Log;
134import android.view.View;
135import android.view.View.OnClickListener;
136import android.widget.Button;
137import android.widget.EditText;
138import android.widget.Toast;
139
140public class MainActivity extends Activity implements OnClickListener {
141
142 Button buttonsubmit;
143 EditText empid,empname,empage,empdept;
144
145 String emp_name, emp_dept;
146 //Integer emp_id,emp_age;
147 Integer emp_age;
148 @Override
149 public void onCreate(Bundle savedInstanceState) {
150 super.onCreate(savedInstanceState);
151 setContentView(R.layout.main);
152
153
154 buttonsubmit = (Button) findViewById(R.id.btnSubmit);
155 buttonsubmit.setOnClickListener(this);
156
157 // empid =(EditText) findViewById(R.id.empid);
158 empname =(EditText) findViewById(R.id.empname);
159 empage =(EditText) findViewById(R.id.empage);
160 empdept =(EditText) findViewById(R.id.empdpt);
161
162
163 }
164
165 @Override
166 public void onClick(View v) {
167 // TODO Auto-generated method stub
168 DataBaseSampleActivity dbObj = new DataBaseSampleActivity(getApplicationContext());
169
170 // String Emp_ids = empid.getText().toString();
171 // emp_id = Integer.parseInt(Emp_ids);
172 //emp_id = empid.getText().toString();
173
174 String Emp_ages = empage.getText().toString();
175 emp_age = Integer.parseInt(Emp_ages);
176
177 //emp_age = empage.getText().toString();
178
179 emp_name = empname.getText().toString();
180 emp_dept = empdept.getText().toString();
181
182
183 try {
184 Log.i("try", "message");
185 dbObj.open();
186 //long temp = dbObj.insert(emp_id, emp_name, emp_age, emp_dept);
187 long temp = dbObj.insert(emp_name, emp_age, emp_dept);
188 //Toast.makeText(getApplicationContext(), "temp"+temp, 3000).show();
189 dbObj.close();
190
191 Intent intent = new Intent(this,ShowListView.class);
192 startActivity(intent);
193 } catch (Exception e) {
194 // TODO: handle exception
195 Log.i("catch", "message");
196 }
197 }
198}
199
200
201
202
203
2042. Create listclass to show tha data
205
206
207package com.db;
208
209import java.lang.reflect.Array;
210import java.util.ArrayList;
211
212import android.R.integer;
213import android.app.Activity;
214import android.content.Context;
215import android.content.Intent;
216import android.database.Cursor;
217import android.os.Bundle;
218import android.util.Log;
219import android.view.LayoutInflater;
220import android.view.View;
221import android.view.View.OnClickListener;
222import android.view.ViewGroup;
223import android.widget.BaseAdapter;
224import android.widget.Button;
225import android.widget.CheckBox;
226import android.widget.CompoundButton;
227import android.widget.CompoundButton.OnCheckedChangeListener;
228import android.widget.ImageView;
229import android.widget.ListView;
230import android.widget.TextView;
231import android.widget.Toast;
232
233public class ShowListView extends Activity {
234
235 ArrayList<String> arrname = new ArrayList<String>();
236 ArrayList<String> arrage = new ArrayList<String>();
237 ArrayList<String> arrdept = new ArrayList<String>();
238 ArrayList<Integer> arrRowId = new ArrayList<Integer>();
239 ArrayList<Integer> arrDelId = new ArrayList<Integer>();
240 Array[] arr;
241 Button deleteBtn;
242 Button btnadd;
243 int index = 0;
244
245 public int pos;
246
247 @Override
248 public void onCreate(Bundle savedInstanceState) {
249 super.onCreate(savedInstanceState);
250 setContentView(R.layout.emplist);
251
252 //Toast.makeText(getApplicationContext(), "LIST VIEW", 5000).show();
253
254 ToGetCursorValues();
255
256 }
257
258 public void ToGetCursorValues(){
259 DataBaseSampleActivity db = new DataBaseSampleActivity(getApplicationContext());
260 db.open();
261 try {
262 Cursor cur = db.getEmpValues();
263 cur.moveToFirst();
264 arrRowId.clear();
265 arrname.clear();
266 arrage.clear();
267 arrdept.clear();
268 while (!cur.isAfterLast()) {
269 arrRowId.add(cur.getInt(cur.getColumnIndex(db.ColEmpID)));
270 arrname.add(cur.getString(cur.getColumnIndex(db.ColEmpName)));
271 arrage.add(cur.getString(cur.getColumnIndex(db.ColEmpAge)));
272 arrdept.add(cur.getString(cur.getColumnIndex(db.ColDept)));
273 cur.moveToNext();
274 }
275 //Log.i("#####","col "+arrlist.size());
276 //Toast.makeText(getApplicationContext(), "* "+arrname.size()+","+arrage.size()+","+arrdept.size(), 5000).show();
277
278 //Toast.makeText(getApplicationContext(), "***** "+arrRowId.get(0), 2000).show();
279
280 } catch (Exception e) {
281 // TODO: handle exception
282 }
283
284
285 ListView lst = (ListView) findViewById(R.id.mylist);
286 lst.setAdapter(new ListAdapter(getApplicationContext()));
287
288 db.close();
289 }
290
291 public class ListAdapter extends BaseAdapter implements OnCheckedChangeListener,OnClickListener{
292 private LayoutInflater inflater = null;
293
294 public ListAdapter(Context c){
295 Log.i("Context","Context");
296 inflater = LayoutInflater.from(c);
297 }
298 @Override
299 public int getCount() {
300 // TODO Auto-generated method stub
301 //return 0;
302 return arrname.size();
303 }
304
305 @Override
306 public Object getItem(int position) {
307 // TODO Auto-generated method stub
308 return null;
309 }
310
311 @Override
312 public long getItemId(int position) {
313 // TODO Auto-generated method stub
314 return 0;
315 }
316 class ViewHolder{
317 TextView empnameview;
318 TextView empageview;
319 TextView empdeptview;
320 CheckBox empchkbox;
321 }
322
323 // create a new ImageView for each item referenced by the Adapter
324 public View getView(final int position, View convertView, ViewGroup parent) {
325
326
327 Log.i("*view","view*");
328 ViewHolder vh;
329 //ImageView imageView;
330 if (convertView == null) { // if it's not recycled, initialize some attributes
331
332 Log.i("*null1*","*null1*");
333
334 vh = new ViewHolder();
335 convertView = inflater.inflate(R.layout.customlist, null);
336
337 Log.i("*null2*","*null2*");
338 pos = position;
339 vh.empnameview = (TextView) convertView.findViewById(R.id.ename);
340 vh.empageview = (TextView) convertView.findViewById(R.id.eage);
341 vh.empdeptview = (TextView) convertView.findViewById(R.id.edept);
342 vh.empchkbox = (CheckBox) convertView.findViewById(R.id.ckekDelete);
343
344 Log.i("*null3*","*null3*");
345
346 vh.empnameview.setText(arrname.get(position));
347
348 vh.empnameview.setOnClickListener(new OnClickListener() {
349
350 @Override
351 public void onClick(View v) {
352 // TODO Auto-generated method stub
353 Intent intent = new Intent(ShowListView.this,UpdateDB.class);
354 String name = arrname.get(position);
355 int age = Integer.parseInt(arrage.get(position));
356 String dept = arrdept.get(position);
357 int rowid = arrRowId.get(position);
358
359 intent.putExtra("KeyName" , name);
360 intent.putExtra("Keyage" , age);
361 intent.putExtra("Keydept" , dept);
362 intent.putExtra("Rowid", rowid);
363
364 startActivity(intent);
365 }
366 });
367
368 vh.empageview.setText(arrage.get(position));
369 vh.empdeptview.setText(arrdept.get(position));
370 vh.empchkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
371
372 @Override
373 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
374 // TODO Auto-generated method stub
375 //Toast.makeText(getApplicationContext(), "OnChecked"+position, 2000).show();
376 if(buttonView.isChecked()){
377 arrDelId.add(arrRowId.get(position));
378 //Toast.makeText(getApplicationContext(), "OnChecked"+position, 2000).show();
379
380// DataBaseSampleActivity db = new DataBaseSampleActivity(getApplicationContext());
381// db.open();
382// db.deleteEmpList(arrRowId.get(position));
383// Toast.makeText(getApplicationContext(), "delet", 3000).show();
384// db.close();
385//
386 }
387 else{
388 for(int i=0;i<arrDelId.size();i++){
389 if(arrRowId.get(position) == arrDelId.get(i)){
390 arrDelId.remove(i);
391 }
392 }
393 }
394 }
395 });
396
397 Log.i("******", "complete");
398
399 } else {
400 Log.i("*not*","*not*");
401 vh = (ViewHolder) convertView.getTag();
402 }
403
404 deleteBtn = (Button) findViewById(R.id.delBtn);
405 deleteBtn.setOnClickListener(this);
406
407 btnadd = (Button) findViewById(R.id.addBtn);
408 btnadd.setOnClickListener(new OnClickListener() {
409
410 @Override
411 public void onClick(View v) {
412 // TODO Auto-generated method stub
413 Intent inte = new Intent(ShowListView.this, MainActivity.class);
414 startActivity(inte);
415 }
416 });
417
418 // imageView.setImageResource(thumbarr[position]);
419 return convertView;
420
421 }
422
423 public View getView1(int position, View convertView, ViewGroup parent) {
424 // TODO Auto-generated method stub
425 return null;
426 }
427
428 @Override
429 public void onClick(View v) {
430 // TODO Auto-generated method stub
431
432 for(int i=0;i<arrDelId.size();i++){
433 //Toast.makeText(getApplicationContext(), "OnDeleteClick "+i, 2000).show();
434 DataBaseSampleActivity db = new DataBaseSampleActivity(getApplicationContext());
435 db.open();
436 db.deleteEmpList(arrDelId.get(i));
437 //Toast.makeText(getApplicationContext(), "delet", 3000).show();
438 db.close();
439 }
440
441 ToGetCursorValues();
442
443 }
444 @Override
445 public void onCheckedChanged(CompoundButton buttonView,
446 boolean isChecked) {
447 // TODO Auto-generated method stub
448
449 }
450
451
452 }
453
454}
455
456
4573. for update
458
459package com.db;
460
461import android.app.Activity;
462import android.content.Intent;
463import android.os.Bundle;
464import android.view.View;
465import android.view.View.OnClickListener;
466import android.widget.Button;
467import android.widget.EditText;
468import android.widget.Toast;
469
470public class UpdateDB extends Activity implements OnClickListener{
471
472 Intent intnt;
473 EditText editname,editage,editdept;
474 Button updateBtn;
475 int row_id;
476
477 @Override
478 public void onCreate(Bundle savedInstanceState) {
479 super.onCreate(savedInstanceState);
480 setContentView(R.layout.main);
481
482 editname = (EditText) findViewById(R.id.empname);
483 editage = (EditText) findViewById(R.id.empage);
484 editdept = (EditText) findViewById(R.id.empdpt);
485
486 updateBtn = (Button) findViewById(R.id.btnSubmit);
487 updateBtn.setText("Update");
488
489 intnt = getIntent();
490
491 editname.setText(intnt.getStringExtra("KeyName"));
492 editage.setText(""+intnt.getIntExtra("Keyage",0));
493 editdept.setText(intnt.getStringExtra("Keydept"));
494
495 row_id = intnt.getIntExtra("Rowid", 0);
496
497 updateBtn.setOnClickListener(new OnClickListener() {
498
499 @Override
500 public void onClick(View v) {
501 // TODO Auto-generated method stub
502 Toast.makeText(getApplicationContext(), "update", 3000).show();
503 DataBaseSampleActivity db = new DataBaseSampleActivity(getApplicationContext());
504 db.open();
505 String empname = editname.getText().toString();
506 int empage = Integer.parseInt(editage.getText().toString());
507 String empdept = editdept.getText().toString();
508 //db.deleteEmpList(row_id);
509 db.updateEmplist(empname, empage, empdept,row_id);
510 //Toast.makeText(getApplicationContext(), "delet", 3000).show();
511 db.close();
512 Intent list = new Intent(UpdateDB.this,ShowListView.class);
513 startActivity(list);
514 }
515 });
516
517 // Toast.makeText(getApplicationContext(), "update "+intnt.getIntExtra("Keyage",0), 3000).show();
518 }
519
520 @Override
521 public void onClick(View v) {
522 // TODO Auto-generated method stub
523
524 }
525
526}