· 9 years ago · Jan 30, 2017, 05:18 PM
1public class MainActivity extends AppCompatActivity {
2
3
4 private ListView mTopList, mDirectReportList;
5 private ProgressBar mProgressBar;
6 private ArrayList<Employee> mEmployees = new ArrayList<>();
7 private BottomListViewAdapter mBottomListViewAdapter;
8 EmployeeDBHandler dbHandler;
9 SQLiteDatabase db;
10 SimpleCursorAdapter simpleCursorAdapter;
11 private int mStartingEmployeeID = startingEmployeeNumber;
12 private String table = "employees";
13 private static final String KEY_ID = "Employee_number";
14
15
16
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.activity_main);
22 dbHandler = new EmployeeDBHandler(getApplicationContext());
23 mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
24 mProgressBar.setVisibility(View.VISIBLE);
25
26
27
28
29
30
31 getXMLData();
32
33
34
35
36 //GUI for seeing android SQLite Database in Chrome Dev Tools
37 Stetho.InitializerBuilder inBuilder = Stetho.newInitializerBuilder(this);
38 inBuilder.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this));
39 Stetho.Initializer in = inBuilder.build();
40 Stetho.initialize(in);
41
42
43
44
45 }
46
47
48 public void getXMLData() {
49 OkHttpClient client = getUnsafeOkHttpClient();
50 Request request = new Request.Builder()
51 .url(getString(R.string.API_FULL_URL))
52 .build();
53 client.newCall(request).enqueue(new Callback() {
54 @Override
55 public void onFailure(Call call, IOException e) {
56 e.printStackTrace();
57 }
58
59
60 @Override
61 public void onResponse(Call call, final Response response) throws IOException {
62 final String responseData = response.body().string();
63 final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
64 final XMLPullParserHandler parserHandler = new XMLPullParserHandler();
65 final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
66
67
68 for (Employee e : employees) {
69 dbHandler.addEmployee(e);
70 }
71
72
73 runOnUiThread(new Runnable() {
74 @Override
75 public void run() {
76 mProgressBar.setVisibility(View.GONE);
77 displayTopList();
78 displayBottomList();
79 }
80 });
81 }
82 });
83 }
84
85
86 public void displayTopList() {
87 Employee singleEmployee = dbHandler.getEmployee(mStartingEmployeeID);
88 Log.i("The first name is: ", singleEmployee.getFirst_name());
89 }
90
91
92 public void displayBottomList() {
93 SQLiteDatabase db = dbHandler.getWritableDatabase();
94 Cursor mBottomListCursor = db.rawQuery("SELECT * FROM employees", null);
95
96
97 ListView mBottomListView = (ListView) findViewById(R.id.mDirectReportList);
98 BottomListViewAdapter bottomAdapter = new BottomListViewAdapter(this, mBottomListCursor);
99 mBottomListView.setAdapter(bottomAdapter);
100 }
101}
102
103public class EmployeeDBHandler extends SQLiteOpenHelper {
104 private static final int DATABASE_VERSION = 1;
105 private static final String DATABASE_NAME = "OneTeam";
106 private static final String TABLE_EMPLOYEE = "employees";
107
108
109 //Employee table columns names
110 private static final String KEY_ID = "Employee_number";
111 private static final String KEY_FIRST_NAME = "First_name";
112 private static final String KEY_LAST_NAME = "Last_name";
113 private static final String KEY_PHONE_NUMBER_MOBILE = "Phone_mobile";
114 private static final String KEY_PHONE_NUMBER_OFFICE = "Phone_office";
115 private static final String KEY_PAYROLL_TITLE = "Payroll_title";
116 private static final String KEY_HAS_DIRECT_REPORTS = "Has_direct_reports";
117 private static final String KEY_EMAIL = "Email";
118 private static final String KEY_COST_CENTER = "Cost_center_id";
119 private static final String KEY_THUMBNAIL_IMAGE = "ThumbnailData";
120 private final static String DB_CLIENTS_ID = "_id";
121
122
123
124
125 public EmployeeDBHandler(Context context) {
126 super(context, DATABASE_NAME, null, DATABASE_VERSION);
127
128
129 }
130
131
132 @Override
133 public void onCreate(SQLiteDatabase db) {
134 String CREATE_EMPLOYEE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_EMPLOYEE + "("
135 + DB_CLIENTS_ID + " INTEGER PRIMARY KEY,"
136 + KEY_ID + " TEXT,"
137 + KEY_FIRST_NAME + " TEXT,"
138 + KEY_LAST_NAME + " TEXT,"
139 + KEY_PHONE_NUMBER_MOBILE + " TEXT,"
140 + KEY_PHONE_NUMBER_OFFICE + " TEXT,"
141 + KEY_PAYROLL_TITLE + " TEXT,"
142 + KEY_HAS_DIRECT_REPORTS + " TEXT,"
143 + KEY_EMAIL + " TEXT,"
144 + KEY_THUMBNAIL_IMAGE + " TEXT,"
145 + KEY_COST_CENTER + " TEXT,"
146 + "UNIQUE(" + KEY_ID + ")"
147 + ")";
148 db.execSQL(CREATE_EMPLOYEE_TABLE);
149 }
150
151
152 @Override
153 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
154 //drop old table if existence
155 db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEE);
156
157
158 //Create table again
159 onCreate(db);
160 }
161
162
163 //Add new employee
164 public boolean addEmployee(Employee employee) {
165 SQLiteDatabase database = this.getWritableDatabase();
166 ContentValues values = new ContentValues();
167 values.put(KEY_ID, employee.getEmployee_number());
168 values.put(KEY_FIRST_NAME, employee.getFirst_name());
169 values.put(KEY_LAST_NAME, employee.getLast_name());
170 values.put(KEY_PHONE_NUMBER_MOBILE, employee.getPhone_mobile());
171 values.put(KEY_PHONE_NUMBER_OFFICE, employee.getPhone_office());
172 values.put(KEY_HAS_DIRECT_REPORTS, employee.getHas_direct_reports());
173 values.put(KEY_EMAIL, employee.getEmail());
174 values.put(KEY_COST_CENTER, employee.getCost_center_id());
175 values.put(KEY_PAYROLL_TITLE, employee.getPayroll_title());
176 values.put(KEY_THUMBNAIL_IMAGE, employee.getThumbnailData());
177
178
179
180
181 //Inserting Row
182 database.replace(TABLE_EMPLOYEE, null, values);
183 database.close();
184 return true;
185 }
186
187
188 //Get single employee
189 public Employee getEmployee(int employeeNumber) {
190 SQLiteDatabase database = this.getReadableDatabase();
191 Employee employee = null;
192
193
194 Cursor cursor = database.query(TABLE_EMPLOYEE, new String[]{
195 KEY_ID, KEY_FIRST_NAME, KEY_LAST_NAME, KEY_PHONE_NUMBER_OFFICE, KEY_PHONE_NUMBER_MOBILE,
196 KEY_HAS_DIRECT_REPORTS, KEY_EMAIL, KEY_COST_CENTER, KEY_PAYROLL_TITLE, KEY_THUMBNAIL_IMAGE}, KEY_ID + "=?",
197 new String[]{String.valueOf(employeeNumber)}, null, null, null, null);
198 if (cursor != null) {
199 if (cursor.moveToFirst()) {
200 employee = new Employee(cursor.getString(0),
201 cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
202 cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
203 cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12),
204 cursor.getString(12), cursor.getString(14), cursor.getString(15), cursor.getString(16),
205 cursor.getString(17), cursor.getString(18), cursor.getString(19), cursor.getString(20),
206 cursor.getString(21), cursor.getString(22), cursor.getString(23), cursor.getString(24),
207 cursor.getString(24), cursor.getString(25), cursor.getString(26));
208 }
209 }
210 cursor.close();
211 database.close();
212 return employee;
213
214
215 }
216
217
218 //Get All Employees
219 public ArrayList<Employee> getAllEmployees() {
220 ArrayList<Employee> employeeList = new ArrayList<>();
221 //Select all query
222 String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
223
224
225 SQLiteDatabase database = this.getWritableDatabase();
226 Cursor cursor = database.rawQuery(selectQuery, null);
227
228
229 //looping through all rows and adding to list
230 if (cursor.moveToFirst()) {
231 do {
232 Employee employee = new Employee();
233 employee.setEmployee_number(cursor.getString(cursor.getColumnIndex(KEY_ID)));
234 employee.setFirst_name(cursor.getString(cursor.getColumnIndex(KEY_FIRST_NAME)));
235 employee.setLast_name(cursor.getString(cursor.getColumnIndex(KEY_LAST_NAME)));
236 employee.setPhone_office(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER_MOBILE)));
237 employee.setPhone_mobile(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER_OFFICE)));
238 employee.setHas_direct_reports(cursor.getString(cursor.getColumnIndex(KEY_HAS_DIRECT_REPORTS)));
239 employee.setEmail(cursor.getString(cursor.getColumnIndex(KEY_EMAIL)));
240 employee.setCost_center_id(cursor.getString(cursor.getColumnIndex(KEY_COST_CENTER)));
241 employee.setPayroll_title(cursor.getString(cursor.getColumnIndex(KEY_PAYROLL_TITLE)));
242 employee.setThumbnailData(cursor.getString(cursor.getColumnIndex(KEY_THUMBNAIL_IMAGE)));
243 } while (cursor.moveToNext());
244 }
245
246
247 //return employees list
248 return employeeList;
249 }
250
251
252 //Get Employee Count
253 public int getEmployeeCount() {
254 String countQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
255 SQLiteDatabase database = this.getReadableDatabase();
256 Cursor cursor = database.rawQuery(countQuery, null);
257 cursor.close();
258
259
260 return cursor.getCount();
261 }
262
263
264 //Updating single employee
265 public int updateEmployee(Employee employee) {
266 SQLiteDatabase database = this.getWritableDatabase();
267
268
269 ContentValues values = new ContentValues();
270 values.put(KEY_FIRST_NAME, employee.getFirst_name());
271 values.put(KEY_LAST_NAME, employee.getLast_name());
272 values.put(KEY_PHONE_NUMBER_MOBILE, employee.getPhone_mobile());
273 values.put(KEY_PHONE_NUMBER_OFFICE, employee.getPhone_office());
274 values.put(KEY_HAS_DIRECT_REPORTS, employee.getHas_direct_reports());
275 values.put(KEY_EMAIL, employee.getEmail());
276 values.put(KEY_COST_CENTER, employee.getCost_center_id());
277 values.put(KEY_PAYROLL_TITLE, employee.getPayroll_title());
278 values.put(KEY_THUMBNAIL_IMAGE, employee.getThumbnailData());
279
280
281 return database.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
282 new String[]{String.valueOf(employee.getEmployee_number())});
283 }
284
285
286 //Delete single employee
287 public void deleteEmployee(Employee employee) {
288 SQLiteDatabase database = this.getWritableDatabase();
289 database.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
290 new String[]{String.valueOf(employee.getEmployee_number())});
291 database.close();
292 }
293
294
295 //delete row
296 public void delete(int id) {
297 SQLiteDatabase db = this.getWritableDatabase();
298 if (db == null) {
299 return;
300 }
301 db.delete(TABLE_EMPLOYEE, "Employee+number = ?", new String[]{String.valueOf(id)});
302 db.close();
303 }
304}