To Lecture Notes

IT 372 -- May 6, 2019

Review Questions

  1. How can you tell in Java code if a radio button or checkbox is checked?
    Ans: Use the isChecked method of the widget.
  2. How can you set an EditText widget to only accept numbers?
    Ans: Set the inputType attribute to "number".
  3. What is the extension of executable file for Android apps?
    Ans: .dex
  4. What is the file extension of the distribution package that contains the executable file and the resource files?
    Ans: .apk
  5. Create an app that tests the onCheckedChange event handler of a Switch widget. Set up an onClickedChange event handler that changes the background color of an ImageView widget  to black or white, depending on if the switch is checked or unchecked. There is no onCheckedChange attribute, so use an anonymous inner class object to implement the event handler.
    Ans: Here are the layout and activity files.

StudentEntryForm Example

Array Adapters

More Android Widgets

Sqlite3 Downloads

Review of SQL

  1. Write SQL statements to create this table named students:

    name   grade    gender
    Alice     6      F
    Jarrod     8      M
    Penny     7      F
    Ryan     6      F

  2. Write queries that show
    1. the entire table. Ans:
      select * from students;
      
    2. all of the girls with age > 7. Ans:
      select * from students
          where gender='F' and age > 7;
      
    3. all the boys sorted by age. Ans:
      select * from students
          where gender="M" order by age;
      
  3. Write SQL statements that
    1. changes the grade of Alice to 7. Ans:
      update students set grade=7 
          where name='Alice';
      
    2. deletes Alice's row. Ans:
      delete from table students
          where name='Alice';
      
    3. deletes the entire students table. Ans:
      drop table students;
      

Creating and Using SQL Tables in Android Apps

  1. Create SQLite database.
    Define a class derived from SQLiteOpenHelper:
    public class SDBH extends SQLiteOpenHelper {
        private static final String DB_NAME = "students.db";
        private static final int DB_VERSION = 1;
    }
    
  2. Create SQLite table:
    db.execSQL("create table students(" +
        "name text, grade integer, gender text);");
    
  3. Insert row into table:
    ContentValues studentValues = new ContentValues( );
    studentValues.put("name", name);
    studentValues.put("grade", grade);
    studentValues.put("gender", gender);
    db.insert("students", null, studentValues);
    
  4. Create Cursor to view query results:
    Cursor cursor = db.query("students",
        new String[ ] {"name", "grade", "gender"},
        null, null, null, null, null);
    
    The five null arguments correspond to these parameters:
    selection  selectionArgs  groupBy  having  orderBy
    
  5. Methods to position cursor within result set:
    cursor.moveToFirst( )
    cursor.moveToNext( )
    cursor.moveToPrevious( )
    cursor.moveToLast( )
    
  6. Methods to obtain field values from current cursor position:
    cursor.getDouble(columnIndex)
    cursor.getInt(columnIndex)
    cursor.getString(columnIndex)
    

StudentsDb1 Example