· 8 years ago · Jul 29, 2018, 02:26 AM
1Poblem when display the sqlite database values in Android Listview
2public class AndroidSQLiteTutorialActivity extends Activity {
3 /** Called when the activity is first created. */
4 ArrayList alarmList = new ArrayList();
5 List<Contact> contacts;
6
7 @Override
8 public void onCreate(Bundle savedInstanceState) {
9 super.onCreate(savedInstanceState);
10 setContentView(R.layout.main);
11
12 ListView mListView = (ListView) findViewById(R.id.listView1);
13 ArrayAdapter mAdapter = new ArrayAdapter<String>(
14 AndroidSQLiteTutorialActivity.this,
15 android.R.layout.simple_list_item_1, alarmList);
16 mListView.setAdapter(mAdapter);
17
18 DatabaseHandler db = new DatabaseHandler(this);
19
20 /**
21 * CRUD Operations
22 * */
23 // Inserting Contacts
24 Log.d("Insert: ", "Inserting ..");
25 db.addContact(new Contact("10", "00", "0", "0", "0", "0", "0", "0"));
26 db.addContact(new Contact("04, "00", "0", "0", "0", "0", "0", "0"));
27 db.addContact(new Contact("05", "00", "0", "0", "0", "0", "0", "0"));
28 db.addContact(new Contact("07", "00", "0", "0", "0", "0", "0", "0"));
29
30 // Reading all contacts
31 Log.d("Reading: ", "Reading all contacts..");
32 contacts = db.getAllContacts();
33
34 for (Contact cn : contacts) {
35 String log = "Id: " + cn.getID() + " ,Hours: " + cn.get_hours()
36 + " ,Minutes: " + cn.get_minutes() + " ,DaysOfWeeks: "
37 + cn.get_daysofweek() + " ,AlarmTime: "
38 + cn.get_alramTime() + " ,Enabled: " + cn.get_enabled()
39 + " ,Vibrate: " + cn.get_vibrate() + " ,Message: "
40 + cn.get_message() + " ,Alert: " + cn.get_alert();
41 // Writing Contacts to log
42 String timeDisplay = cn.get_hours() + ":" + cn.get_minutes();
43 alarmList.add(timeDisplay);
44 Log.d("Name: ", log);
45
46 }
47 }
48}
49
50public class DatabaseHandler extends SQLiteOpenHelper {
51
52 // All Static variables
53 // Database Version
54 private static final int DATABASE_VERSION = 3;
55
56 // Database Name
57 private static final String DATABASE_NAME = "contactsManager";
58
59 // Contacts table name
60 private static final String TABLE_CONTACTS = "contacts";
61
62 // Contacts Table Columns names
63 private static final String KEY_ID = "id";
64 public static final String KEY_HOUR = "hours";
65 public static final String KEY_MINUTES = "minutes";
66 public static final String KEY_DAYSOFWEEK = "daysofweek";
67 public static final String KEY_ALARMTIME = "alramtime";
68 public static final String KEY_ENABLED = "enabled";
69 public static final String KEY_VIBRATE = "vibrate";
70 public static final String KEY_MESSAGE = "message";
71 public static final String KEY_ALERT = "alert";
72
73 public DatabaseHandler(Context context) {
74 super(context, DATABASE_NAME, null, DATABASE_VERSION);
75 }
76
77 // Creating Tables
78 @Override
79 public void onCreate(SQLiteDatabase db) {
80 String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS "
81 + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
82 + KEY_HOUR + " TEXT," + KEY_MINUTES + " TEXT," + KEY_DAYSOFWEEK
83 + " TEXT," + KEY_ALARMTIME + " TEXT," + KEY_ENABLED + " TEXT,"
84 + KEY_VIBRATE + " TEXT," + KEY_MESSAGE + " TEXT," + KEY_ALERT
85 + " TEXT" + ")";
86
87 db.execSQL(CREATE_CONTACTS_TABLE);
88 }
89
90 // Upgrading database
91 @Override
92 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
93 // Drop older table if existed
94 db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
95
96 // Create tables again
97 onCreate(db);
98 }
99
100 /**
101 * All CRUD(Create, Read, Update, Delete) Operations
102 */
103
104 // Adding new contact
105 void addContact(Contact contact) {
106 SQLiteDatabase db = this.getWritableDatabase();
107
108 ContentValues values = new ContentValues();
109 values.put(KEY_HOUR, contact.get_hours());
110 values.put(KEY_MINUTES, contact.get_minutes());
111 values.put(KEY_DAYSOFWEEK, contact.get_daysofweek());
112 values.put(KEY_ALARMTIME, contact.get_alramTime());
113 values.put(KEY_ENABLED, contact.get_enabled());
114 values.put(KEY_VIBRATE, contact.get_vibrate());
115 values.put(KEY_MESSAGE, contact.get_message());
116 values.put(KEY_ALERT, contact.get_alert());
117
118 // Inserting Row
119 db.insert(TABLE_CONTACTS, null, values);
120 db.close(); // Closing database connection
121 }
122
123 // Getting single contact
124 Contact getContact(int id) {
125 SQLiteDatabase db = this.getReadableDatabase();
126
127 Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
128 KEY_HOUR, KEY_MINUTES, KEY_DAYSOFWEEK, KEY_ALARMTIME,
129 KEY_ENABLED, KEY_VIBRATE, KEY_MESSAGE, KEY_ALERT }, KEY_ID
130 + "=?", new String[] { String.valueOf(id) }, null, null, null,
131 null);
132 if (cursor != null)
133 cursor.moveToFirst();
134
135 Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
136 cursor.getString(1), cursor.getString(2), cursor.getString(3),
137 cursor.getString(4), cursor.getString(5), cursor.getString(6),
138 cursor.getString(7), cursor.getString(8));
139 // return contact
140 return contact;
141 }
142
143 // Getting All Contacts
144 public List<Contact> getAllContacts() {
145 List<Contact> contactList = new ArrayList<Contact>();
146 // Select All Query
147 String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
148
149 SQLiteDatabase db = this.getWritableDatabase();
150 Cursor cursor = db.rawQuery(selectQuery, null);
151
152 // looping through all rows and adding to list
153 if (cursor.moveToFirst()) {
154 do {
155 Contact contact = new Contact();
156 contact.setID(Integer.parseInt(cursor.getString(0)));
157 contact.set_hours(cursor.getString(1));
158 contact.set_minutes(cursor.getString(2));
159 contact.set_daysofweek(cursor.getString(3));
160 contact.set_alramTime(cursor.getString(4));
161 contact.set_enabled(cursor.getString(5));
162 contact.set_vibrate(cursor.getString(6));
163 contact.set_message(cursor.getString(7));
164 contact.set_alert(cursor.getString(8));
165
166 // Adding contact to list
167 contactList.add(contact);
168 } while (cursor.moveToNext());
169 }
170
171 // return contact list
172 return contactList;
173 }
174
175 // Updating single contact
176 public int updateContact(Contact contact) {
177 SQLiteDatabase db = this.getWritableDatabase();
178
179 ContentValues values = new ContentValues();
180 values.put(KEY_HOUR, contact.get_hours());
181 values.put(KEY_MINUTES, contact.get_minutes());
182 values.put(KEY_DAYSOFWEEK, contact.get_daysofweek());
183 values.put(KEY_ALARMTIME, contact.get_alramTime());
184 values.put(KEY_ENABLED, contact.get_enabled());
185 values.put(KEY_VIBRATE, contact.get_vibrate());
186 values.put(KEY_MESSAGE, contact.get_message());
187 values.put(KEY_ALERT, contact.get_alert());
188
189 // updating row
190 return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
191 new String[] { String.valueOf(contact.getID()) });
192 }
193
194 // Deleting single contact
195 public void deleteContact(Contact contact) {
196 SQLiteDatabase db = this.getWritableDatabase();
197 db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
198 new String[] { String.valueOf(contact.getID()) });
199 db.close();
200 }
201
202 // Getting contacts Count
203 public int getContactsCount() {
204 String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
205 SQLiteDatabase db = this.getReadableDatabase();
206 Cursor cursor = db.rawQuery(countQuery, null);
207 cursor.close();
208
209 // return count
210 return cursor.getCount();
211 }
212
213}
214
215public class Contact {
216
217
218
219 public String get_alramTime() {
220 return this._alramTime;
221 }
222
223 public void set_alramTime(String _alramTime) {
224 this._alramTime = _alramTime;
225 }
226
227 public String get_enabled() {
228 return this._enabled;
229 }
230
231 public void set_enabled(String _enabled) {
232 this._enabled = _enabled;
233 }
234
235 public String get_vibrate() {
236 return this._vibrate;
237 }
238
239 public void set_vibrate(String _vibrate) {
240 this._vibrate = _vibrate;
241 }
242
243 public String get_message() {
244 return this._message;
245 }
246
247 public void set_message(String _message) {
248 this._message = _message;
249 }
250
251 public String get_alert() {
252 return this._alert;
253 }
254
255 public void set_alert(String _alert) {
256 this._alert = _alert;
257 }
258
259 public String get_hours() {
260 return this._hours;
261 }
262
263 public void set_hours(String _hours) {
264 this._hours = _hours;
265 }
266
267 public String get_minutes() {
268 return this._minutes;
269 }
270
271 public void set_minutes(String _minutes) {
272 this._minutes = _minutes;
273 }
274
275 public String get_daysofweek() {
276 return this._daysofweek;
277 }
278
279 public void set_daysofweek(String _daysofweek) {
280 this._daysofweek = _daysofweek;
281 }
282
283 // private variables
284 int _id;
285 String _hours;
286 String _minutes;
287 String _daysofweek;
288 String _alramTime;
289 String _enabled;
290 String _vibrate;
291 String _message;
292 String _alert;
293
294 // Empty constructor
295 public Contact() {
296
297 }
298
299 // constructor
300 public Contact(int id, String hours, String minutes, String daysofweek,
301 String alarmtime, String enabled, String vibrate, String message,
302 String alert) {
303 this._id = id;
304 this._hours = hours;
305 this._minutes = minutes;
306 this._daysofweek = daysofweek;
307 this._alramTime = alarmtime;
308 this._enabled = enabled;
309 this._vibrate = vibrate;
310 this._message = message;
311 this._alert = alert;
312 }
313
314 // constructor
315 public Contact(String hours, String minutes, String daysofweek,
316 String alarmtime, String enabled, String vibrate, String message,
317 String alert) {
318 this._hours = hours;
319 this._minutes = minutes;
320 this._daysofweek = daysofweek;
321 this._alramTime = alarmtime;
322 this._enabled = enabled;
323 this._vibrate = vibrate;
324 this._message = message;
325 this._alert = alert;
326
327 }
328
329 // getting ID
330 public int getID() {
331 return this._id;
332 }
333
334 // setting id
335 public void setID(int id) {
336 this._id = id;
337 }
338
339}
340
341db.addContact(new Contact("10", "00", "0", "0", "0", "0", "0", "0"));
342db.addContact(new Contact("04, "00", "0", "0", "0", "0", "0", "0"));
343db.addContact(new Contact("05", "00", "0", "0", "0", "0", "0", "0"));
344db.addContact(new Contact("07", "00", "0", "0", "0", "0", "0", "0"));
345
346//...
347db.execSQL(CREATE_CONTACTS_TABLE);
348ArrayList<Contact> toInsert = new ArrayList<Contact>();
349toInsert.add(new Contact("10", "00", "0", "0", "0", "0", "0", "0"));
350toInsert.add(new Contact("04, "00", "0", "0", "0", "0", "0", "0"));
351toInsert.add(new Contact("05", "00", "0", "0", "0", "0", "0", "0"));
352toInsert.add(new Contact("07", "00", "0", "0", "0", "0", "0", "0"));
353for (int i = 0; i < toInsert.size(); i++) {
354 ContentValues values = new ContentValues();
355 values.put(KEY_HOUR, toInsert.get(i).get_hours());
356 values.put(KEY_MINUTES, toInsert.get(i).get_minutes());
357 values.put(KEY_DAYSOFWEEK, toInsert.get(i).get_daysofweek());
358 values.put(KEY_ALARMTIME, toInsert.get(i).get_alramTime());
359 values.put(KEY_ENABLED, toInsert.get(i).get_enabled());
360 values.put(KEY_VIBRATE, toInsert.get(i).get_vibrate());
361 values.put(KEY_MESSAGE, toInsert.get(i).get_message());
362 values.put(KEY_ALERT, toInsert.get(i).get_alert());
363 db.insert(TABLE_CONTACTS, null, values);
364}
365
366db.addContact(new Contact("10", "00", "0", "0", "0", "0", "0", "0"));
367 db.addContact(new Contact("04, "00", "0", "0", "0", "0", "0", "0"));
368 db.addContact(new Contact("05", "00", "0", "0", "0", "0", "0", "0"));
369 db.addContact(new Contact("07", "00", "0", "0", "0", "0", "0", "0"));