· 9 years ago · Oct 03, 2016, 07:22 PM
1package a.vaccination;
2
3import android.annotation.TargetApi;
4import android.content.ContentValues;
5import android.content.Context;
6import android.database.Cursor;
7import android.database.SQLException;
8import android.database.sqlite.SQLiteDatabase;
9import android.database.sqlite.SQLiteOpenHelper;
10import android.os.Build;
11import android.util.Log;
12
13@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
14public class datahandler {
15
16 public static final String KEY_ROWID = "id";
17 public static final String KEY_name = "name";
18 public static final String KEY_dob = "dob";
19 public static final String KEY_contact = "contact";
20 public static final String KEY_email = "email";
21 private static final String TAG = "DBAdapter";
22
23 private static final String DATABASE_NAME = "profile";
24 private static final String DATABASE_TABLE = "baby";
25 private static final int DATABASE_VERSION = 2;
26
27 private static final String DATABASE_CREATE = "create table if not exists"
28 + DATABASE_TABLE
29 + "(id integer primary key autoincrement, "
30 + "name VARCHAR not null, dob date, contact VARCHAR, email
31VARCHAR);";
32 private final Context context;
33
34 private DatabaseHelper DBHelper;
35 private SQLiteDatabase db;
36
37 public datahandler(Context ctx) {
38 this.context = ctx;
39 DBHelper = new DatabaseHelper(context);
40 }
41
42 protected static class DatabaseHelper extends SQLiteOpenHelper {
43 DatabaseHelper(Context context) {
44 super(context, DATABASE_NAME, null, DATABASE_VERSION);
45 }
46
47 @Override
48 public void onCreate(SQLiteDatabase db) {
49 // TODO Auto-generated method stub
50 try {
51 db.execSQL(DATABASE_CREATE);
52 // db = DBHelper.getWritableDatabase();
53 Log.i(DATABASE_NAME, "create");
54 } catch (SQLException e) {
55 e.printStackTrace();
56 }
57 }
58
59 @Override
60 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
61 // TODO Auto-generated method stub
62 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
63 + newVersion + ", which will destroy all old
64data");
65 db.execSQL("DROP TABLE IF EXISTS contacts");
66 onCreate(db);
67 }
68 }
69
70 // ---opens the database---
71 public datahandler open() throws SQLException {
72 db = DBHelper.getWritableDatabase();
73 return this;
74 }
75
76 // ---closes the database---
77 public void close() {
78 DBHelper.close();
79 }
80
81 // ---insert a record into the database---
82 public long insertRecord(String name, String dob, String contact,
83 String email) {
84 ContentValues initialValues = new ContentValues();
85 initialValues.put(KEY_name, name);
86 initialValues.put(KEY_dob, dob);
87 initialValues.put(KEY_contact, contact);
88 initialValues.put(KEY_email, email);
89 Log.i(DATABASE_NAME, "values added");
90 return db.insert(DATABASE_TABLE, null, initialValues);
91
92 }
93
94 // ---updates a record---
95 public boolean updateRecord(long rowId, String name, String dob,
96 String contact, String email) {
97 ContentValues args = new ContentValues();
98 args.put(KEY_name, name);
99 args.put(KEY_dob, dob);
100 args.put(KEY_contact, contact);
101 args.put(KEY_email, email);
102 return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
103 }
104
105 public Cursor getAllTitles() {
106 return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_name,
107 KEY_dob, KEY_contact, KEY_email }, null, null, null, null,
108 null, null);
109 }
110
111 // ---retrieves a particular title---
112
113 public Cursor getTitle(long rowId) throws SQLException {
114 Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
115 KEY_ROWID, KEY_name, KEY_email, KEY_dob, KEY_contact },
116 KEY_ROWID + "=" + rowId, null, null, null, null, null,
117null);
118 if (mCursor != null) {
119 mCursor.moveToFirst();
120 }
121 return mCursor;
122 }
123
124 public void getReadableDatabase() {
125 // TODO Auto-generated method stub
126 Cursor cur = db
127 .rawQuery("select rowid _id,* from DATABASE_TABLE", null);
128 }
129}
130
131package a.vaccination;
132
133import android.app.Activity;
134import android.content.Intent;
135import android.database.Cursor;
136import android.os.Bundle;
137import android.support.v4.widget.SimpleCursorAdapter;
138import android.view.View;
139import android.view.View.OnClickListener;
140import android.widget.AdapterView;
141import android.widget.AdapterView.OnItemClickListener;
142import android.widget.Button;
143import android.widget.ListView;
144import android.widget.Toast;
145
146//this shows profies of baby and option of add new one
147public class profilelist extends Activity implements OnItemClickListener {
148
149 datahandler db = new datahandler(this);
150 SimpleCursorAdapter adapter = null;
151 Cursor c;
152
153 @SuppressWarnings("deprecation")
154 @Override
155 protected void onCreate(Bundle savedInstanceState) {
156 super.onCreate(savedInstanceState);
157 setContentView(R.layout.child);
158
159 Button add = (Button) findViewById(R.id.button1);
160 add.setOnClickListener(new OnClickListener() {
161
162 @Override
163 public void onClick(View v) {
164 // navigate to create profile
165 Intent it = new Intent(profilelist.this,
166createprofile.class);
167 startActivity(it);
168
169 }
170 });
171
172
173 db.open();
174 Cursor c = db.getAllTitles();
175 if (c.moveToFirst()) {
176 do {
177 DisplayRecord(c);
178 } while (c.moveToNext());
179 }
180 db.close();
181
182 // ---get a Record---
183 db.open();
184 c = db.getTitle(2);
185 if (c.moveToFirst())
186 DisplayRecord(c);
187 else
188 Toast.makeText(this, "No Assignments found", Toast.LENGTH_LONG)
189 .show();
190 db.close();
191
192 }
193
194 public void DisplayRecord(Cursor c) {
195 // TODO Auto-generated method stub
196 Toast.makeText(
197 this,
198 "id: " + c.getString(0) + "n" + "name: " + c.getString(1)
199 + "n" + " dob: " + c.getString(2) +
200"contact"
201 + c.getString(3) + "email" +
202c.getString(4),
203
204 Toast.LENGTH_SHORT).show();
205 String[] columns = new String[] { datahandler.KEY_name };
206 int[] to = new int[] { R.id.name };
207 adapter = new SimpleCursorAdapter(this, R.layout.child, c, columns,
208 to, 0);
209 ListView names = (ListView) findViewById(R.id.listView1);
210 names.setAdapter(adapter);
211 names.setOnItemClickListener(this);
212
213 }
214
215Intent i = new Intent(profilelist.this, vaccination.class);
216 startActivity(i);
217 }
218
219
220}