Download presentation
Presentation is loading. Please wait.
Published byBriana Johnson Modified over 8 years ago
1
Database Programming Code Dissection
2
Layered Approach Presentation (Activity) DbSampleActivity.java DataAccess (DataSource) CommentsDataSource.java MySQLiteHelper.java Object Class (Entity) Comment.java
3
Activity (onCreate) public void onCreate() { instantiate and open datasource setupViews addButtonListener updateTable }
4
Activity (setupViews) dataTable = (TableLayout)findViewById(R.id.data_table); txtcomment = (EditText)findViewById(R.id.txtComment); txtauthor = (EditText)findViewById(R.id.txtAuthor); txtdelauthor = (EditText)findViewById(R.id.txtDelAuthor); btnadd = (Button)findViewById(R.id.btnAdd); btndelete = (Button)findViewById(R.id.btnDelete);
5
Activity (addButtonListener) btnadd.setOnClickListener(this); btndelete.setOnClickListener(this);
6
Activity (updateTable) while (dataTable.getChildCount() > 1) { dataTable.removeViewAt(1); } ArrayList > data = datasource.getAllRowsAsArrays(); for (int position=0; position < data.size(); position++) { TableRow tableRow= new TableRow(this); ArrayList row = data.get(position); TextView txtcomment = new TextView(this); txtcomment.setText(row.get(0).toString()); tableRow.addView(txtcomment); TextView txtauthor = new TextView(this); txtauthor.setText(row.get(1).toString()); tableRow.addView(txtauthor); dataTable.addView(tableRow); }
7
Activity (updateTable) while table has rows { remove first row } place data into an ArrayList ***ArrayList of ArrayList Objects*** loop based on the number of rows of data { instantiate a new TableRow Layout place the data of the current row into an ArrayList Object assign the row values into TextViews add the TextViews into the TableRow add the view to the display screen }
8
Activity (onClick) public void onClick(View view) { If add then datasource.createComment(fields); updateTable(); If delete then datasource.deleteComment(field); updateTable(); }
9
DataSource(createComment) public void createComment(String comment, String author) { ContentValues values = new ContentValues(); values.put(MySQLiteHelper.COLUMN_COMMENT, comment); values.put(MySQLiteHelper.COLUMN_AUTHOR, author); try { database.insert(MySQLiteHelper.TABLE_COMMENTS, null, values); } catch(Exception e) { Log.e("DB ERROR", e.toString()); e.printStackTrace(); }
10
DataSource(createComment) ContentValues used to insert new rows into tables each Content Value object represents a single table row as a map of column names to values
11
DataSource (getAllRowsAsArrays) public ArrayList > getAllRowsAsArrays() { ArrayList > dataArrays = new ArrayList >(); Cursor cursor; try { cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, new String[]{MySQLiteHelper.COLUMN_COMMENT, MySQLiteHelper.COLUMN_AUTHOR}, null, null, null, null, null); cursor.moveToFirst(); if (!cursor.isAfterLast()) { do { ArrayList dataList = new ArrayList (); dataList.add(cursor.getString(0)); dataList.add(cursor.getString(1)); dataArrays.add(dataList); } while (cursor.moveToNext());} } catch (SQLException e) { Log.e("DB Error", e.toString()); e.printStackTrace(); } return dataArrays; }
12
DataSource (getAllRowsAsArrays) Cursors Queries in Android are returned as Cursor objects pointers to the result set within the underlying data provide a managed way of controlling your position (row) in the result set of a database query
13
SQLite Helper Class Database Schema database description declaration of table name and fields
14
SQLite Helper Class SQL SELECT INSERT UPDATE DELETE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.