· 9 years ago · Mar 22, 2017, 12:24 PM
1GroupWorkProject
2
3ContentAdapter.java
4
5package com.example.liam.groupworkproject;
6
7import android.content.Context;
8import android.widget.*;
9import android.view.*;
10
11import java.util.ArrayList;
12
13public class ContentAdapter extends BaseAdapter {
14 private Context mContext;
15
16
17 public ContentAdapter(Context c, ArrayList<Student> students) {
18 mContext = c;
19 this.students = students;
20 }
21
22 public int getCount() {
23 return students.size();
24 }
25
26 public Object getItem(int position) {
27 return null;
28 }
29
30 public long getItemId(int position) {
31 return students.get(position).getStdID();
32 }
33
34 // create a new TextView for each item referenced by the Adapter
35 public View getView(int position, View convertView, ViewGroup parent) {
36 TextView textView = new TextView(mContext);
37
38 textView.setText(students.get(position).toString());
39 textView.setTextSize(24);
40
41 return textView;
42 }
43
44 // references to the student records
45 private ArrayList<Student> students;
46}
47
48MainActivity.java
49
50package com.example.liam.groupworkproject;
51
52import android.support.v7.app.AppCompatActivity;
53import android.os.Bundle;
54import android.util.Log;
55import android.widget.ListView;
56
57import java.util.ArrayList;
58
59public class MainActivity extends AppCompatActivity {
60 // Database Helper
61 MySQLiteOpenHelper db;
62 @Override
63 protected void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65 setContentView(R.layout.activity_main);
66// creating an instance of the DB helper class
67 db = new MySQLiteOpenHelper(getApplicationContext());
68 db.insertRecord(12345678, "Edmond", "Ho", "edho@gmail.com", "0338383");
69 db.insertRecord(24681012, "Desmond", "Ho", "lol@gmail.com", "0334834");
70 db.insertRecord(98765432, "Raymond", "Ho", "hahahaha@gmail.com", "3948349");
71 ArrayList<Student> students = db.retrieveAllRecords();
72 ListView listView = (ListView) findViewById(R.id.listView);
73 listView.setAdapter(new ContentAdapter(getApplicationContext(), students));
74 }
75}
76
77MySQLiteOpenHelper.java
78
79package com.example.liam.groupworkproject;
80
81import android.content.ContentValues;
82import android.content.Context;
83import android.database.Cursor;
84import android.database.sqlite.SQLiteDatabase;
85import android.database.sqlite.SQLiteOpenHelper;
86
87import java.util.ArrayList;
88
89public class MySQLiteOpenHelper extends SQLiteOpenHelper {
90 private static final String DB_NAME = "northumbria";
91 private static final int DB_VERSION = 2;
92 private static final String TABLE_NAME = "student_info";
93 private static final String STD_ID = "id";
94 private static final String FIRST_NAME = "first_name";
95 private static final String LAST_NAME = "last_name";
96 private static final String EMAIL = "email";
97 public static final String contNUMBER = "contact_number";
98 //private static final String CREATE_TABLE_STAT = "create table "+TABLE_NAME+" ("+STD_ID+" INTEGER PRIMARY KEY, "+FIRST_NAME+" TEXT, "+LAST_NAME+" TEXT)";
99 private static final String CREATE_TABLE_STAT = "create table "+TABLE_NAME+" ("+STD_ID+" INTEGER PRIMARY KEY, "+FIRST_NAME+" BLOB, "+LAST_NAME+" BLOB, "+EMAIL+"BLOB, "+contNUMBER+")";
100 private static String[] allColumns = { STD_ID, FIRST_NAME, LAST_NAME, EMAIL, contNUMBER};
101
102 public MySQLiteOpenHelper(Context applicationContext) {
103 super(applicationContext, DB_NAME, null, DB_VERSION);
104 }
105
106
107 // insert new record to the table
108 public void deleteRecord(int student_id) {
109 SQLiteDatabase db = this.getWritableDatabase();
110
111 db.delete(TABLE_NAME, STD_ID+"=?", new String[] { Integer.toString(student_id) });
112 }
113
114
115 // insert new record to the table
116 public void insertRecord(int student_id, String firstName, String lastName, String contEmail, String contNum) {
117 SQLiteDatabase db = this.getWritableDatabase();
118
119 // setup name-value pairs
120 ContentValues values = new ContentValues();
121 values.put(STD_ID, student_id);
122 values.put(FIRST_NAME, firstName.getBytes());
123 values.put(LAST_NAME, lastName.getBytes());
124 values.put(EMAIL, contEmail.getBytes());
125 values.put(contNUMBER, contNum.getBytes());
126
127 // insert row to the database
128 db.insert(TABLE_NAME, null, values);
129 }
130
131 // retrieve all records from the table
132 public ArrayList<Student> retrieveAllRecords() {
133 SQLiteDatabase db = this.getReadableDatabase();
134
135 ArrayList<Student> students = new ArrayList<Student>();
136
137 // retrieve all records and order them by student ID in ascending order
138 Cursor c = db.query(TABLE_NAME, allColumns, null, null, null, null, STD_ID);
139
140 // loop through every record, start with the first one
141 c.moveToFirst();
142 while (!c.isAfterLast()) { // not pointing after the last record
143 // the order of the values in teh table are ID, first_name, and last_name
144 // So, the array indices are 0, 1, and 2 respectively
145 Student newStudent = new Student(c.getInt(0), new String(c.getBlob(1)), new String(c.getBlob(2)), new String(c.getBlob(3)), new String(c.getBlob(4)));
146
147 // add the new record to the arrayList
148 students.add(newStudent);
149
150 c.moveToNext(); // move to next record
151 }
152
153 return students;
154 }
155
156 // delete all records
157 public void deleteAllRecords() {
158 SQLiteDatabase db = this.getWritableDatabase();
159
160 db.delete(TABLE_NAME, null, null);
161 }
162
163
164 // to create the student information table
165 public void onCreate(SQLiteDatabase db) {
166 db.execSQL(CREATE_TABLE_STAT);
167 }
168
169 @Override
170 public void onUpgrade(SQLiteDatabase db, int i, int i1) {
171 // remove table in previous version (already in the database)
172 db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
173
174 // create table in the current version
175 onCreate(db);
176 }
177}
178
179
180Student.java
181
182package com.example.liam.groupworkproject;
183
184public class Student {
185 private String firstName;
186 private String lastName;
187 private String email;
188 private String number;
189 private int ID;
190
191 // constructor
192 public Student(int ID, String firstName, String lastName, String contEmail, String contNumber) {
193 this.firstName = firstName;
194 this.lastName = lastName;
195 this.email = contEmail;
196 this.number = contNumber;
197 this.ID = ID;
198 }
199
200 // create formatted string from the class variables
201 public String toString() {
202 return firstName + " " + lastName + " (" + ID + ")" + email + " " + number;
203 }
204
205 // getter method for the ID field
206 public int getStdID() {
207 return ID;
208 }
209}