ITWB312 Reviewer Finals PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides a guide on using SQLite database in an Android application, covering key components, code walkthrough, and database operations. It aims to explain the functionalities and importance of SQLiteOpenHelper, ContentValues, Cursor, and SQLiteDatabase classes.
Full Transcript
SQLite Application Code Reviewer Guide This guide explains the importance of each code section, identifies key terms, and makes it easier to understand their role in building an Android application with SQLite database integration. 1\. Key Components Overview SQLiteOpenHelper: Simplifies database...
SQLite Application Code Reviewer Guide This guide explains the importance of each code section, identifies key terms, and makes it easier to understand their role in building an Android application with SQLite database integration. 1\. Key Components Overview SQLiteOpenHelper: Simplifies database creation and version management. ContentValues: Helps store a set of key-value pairs for database operations like insert and update. Cursor: Manages database query results. SQLiteDatabase: Provides methods for database CRUD (Create, Read, Update, Delete) operations. 2\. Code Walkthrough and Explanation DatabaseHelper Class This class manages the database, its tables, and CRUD operations. Key Terms: SQLiteOpenHelper: Base class for managing SQLite databases. onCreate: Triggered when the database is created for the first time. onUpgrade: Handles schema changes between database versions. Code Explanation: Database Properties: public static final String DATABASE\_NAME = \"Student.db\"; public static final String TABLE\_NAME = \"student\_table\"; Defines the database and table names as constants for reuse and consistency. Table Schema Definition: dbDefinition = \"CREATE TABLE \" + TABLE\_NAME + \"(ID integer PRIMARY KEY AUTOINCREMENT, StudentID Text, FirstName Text, LastName Text)\"; SQL command for creating the table with columns (ID, StudentID, FirstName, LastName). Database Operations: Insert Data: contentValues.put(COL\_2, stNumber); long result = saveCmd.insert(TABLE\_NAME, null, contentValues); Prepares data and inserts it into the database. Returns true if successful. Read Data: resultSet = viewRecords.rawQuery(\"SELECT \* FROM \" + TABLE\_NAME, null); Executes an SQL query to retrieve all records. Update Data: editRecord.update(TABLE\_NAME, contentValues, \"StudentID = ?\", new String\[\] { studentID }); Updates a specific record based on StudentID. Delete Data: db.delete(TABLE\_NAME, \"StudentID = ?\", new String\[\] { id }); Deletes a record where StudentID matches the given value. MainActivity Class This class connects the database to the app\'s UI and handles user interactions. Key Terms: EditText: Input field for user data. GridView: Displays database records in a tabular format. onClick: Event handler triggered by button clicks. Code Explanation: Initialize UI Components: txtStudentID = findViewById(R.id.editStudentID); txtFName = findViewById(R.id.editFName); Maps XML-defined UI elements to Java objects for manipulation. Add Record: boolean isRecordSaved = myData.SaveNewRecord(txtStudentID.getText().toString(), txtFName.getText().toString(), txtLName.getText().toString()); Saves a new record in the database if it doesn't already exist. View Records: ArrayList\ myList = new ArrayList\(); Cursor myDataResult = myData.ViewRecords(); while (myDataResult.moveToNext()) { myList.add(myDataResult.getString(0)); // Adds each column value to the list. } Retrieves all records and displays them in a GridView. Edit Record: myData.UpdateRecord(textRecordID.getText().toString(), txtStudentID.getText().toString(), \...); Updates the record with the given textRecordID. Delete Record: int deletedRows = myData.deleteData(txtStudentID.getText().toString()); Deletes a record based on StudentID. Sample UI Layout The XML file defines the visual structure of the app. Key Terms: LinearLayout: Arranges child views in a vertical or horizontal sequence. EditText: Input fields for user data. Button: Triggers specific actions in the app. Code Highlights: Input Fields: \ Input for Student ID, First Name, and Last Name. Action Buttons: \ Buttons for actions like Add, Edit, View, Search, and Delete. GridView: \ Displays database records in a structured format. 3\. Importance of This Code Scalable Database Management: Handles data efficiently using SQLite, ideal for small apps. User Interaction: Provides CRUD functionality directly from the UI. Learning Opportunity: Demonstrates practical use of SQLite and Android development concepts. **Summary of Key Terms** **Key Term** **Definition** ------------------ -------------------------------------------------------- SQLiteOpenHelper Base class for managing database creation and updates. onCreate Called when the database is first created. ContentValues Holds key-value pairs for insert/update operations. Cursor Retrieves data from database queries. SQLiteDatabase Provides methods for interacting with the database. LinearLayout Arranges child views vertically or horizontally. EditText Input field for user-provided data. GridView Displays data in a tabular format. AlertDialog Shows pop-up messages for user interaction. MODULE 5 Advanced Controls: AutoCompleteTextView The AutoCompleteTextView is a view that extends the EditText widget, allowing it to display a list of suggestions automatically as the user types. This control is particularly useful for implementing features like search bars, form inputs, or any text field where predictive text suggestions improve the user experience. ### Attributes and Descriptions **Sr. No.** **Attribute** **Description** ------------- ---------------------------------- ----------------------------------------------------------------------------------------- 1 android:completionHint Specifies the hint displayed in the dropdown menu. 2 android:completionHintView Defines the custom view for the hint displayed in the dropdown menu. 3 android:completionThreshold Determines the minimum number of characters a user must type before suggestions appear. 4 android:dropDownAnchor Specifies the view to which the autocomplete dropdown is anchored. 5 android:dropDownHeight Sets the height of the dropdown menu. 6 android:dropDownHorizontalOffset Specifies the horizontal offset (in pixels) for the dropdown. 7 android:dropDownSelector Defines the selector drawable used in the dropdown list. 8 android:dropDownVerticalOffset Specifies the vertical offset (in pixels) for the dropdown. 9 android:dropDownWidth Sets the width of the dropdown menu. 10 android:popupBackground Defines the background of the dropdown menu. ### Key Components in the Java Code **Component** **Description** ------------------------------- ---------------------------------------------------------------------------- AutoCompleteTextView A specialized EditText that shows suggestion dropdowns while typing. ArrayAdapter Bridges the array of data (arr) with the dropdown suggestions. setThreshold(int n) Sets the minimum number of characters before suggestions appear (e.g., 2). setAdapter(adapter) Links the ArrayAdapter to the AutoCompleteTextView for data binding. R.layout.select\_dialog\_item Specifies the layout for each dropdown item in the list. **Key XML Attributes** **Attribute** **Description** --------------------------- --------------------------------------------------------------------------------------- android:layout\_width Sets the width of the widget (e.g., wrap\_content, match\_parent). android:layout\_height Sets the height of the widget. android:id Assigns a unique identifier to the widget for referencing in Java code. android:ems Sets the width of the AutoCompleteTextView in terms of the number of text characters. android:layout\_below Positions the widget below another widget using its ID. android:layout\_marginTop Adds spacing above the widget for better UI alignment. ### **Key Components** **Component** **Description** -------------------- -------------------------------------------------------------------------------------- ToggleButton A button that toggles between two states (ON/OFF). setOnClickListener Registers a click listener to execute code when the Button is pressed. getText() Retrieves the current text of the ToggleButton based on its state (e.g., ON or OFF). StringBuffer Used to dynamically append text strings for the Toast message. Toast.makeText Displays a short message (Toast) to the user with the toggle states. ### **How It Works** 1. **ToggleButtons**: - Users can toggle between **ON** and **OFF** states for both buttons. - The text displayed updates dynamically based on the current state. 2. **Button Action**: - When the **Click Me** button is pressed, it retrieves the states of both ToggleButtons using getText() and displays them in a Toast. 3. **TimePicker - Methods and Descriptions** **Sr. No** **Method** **Description** ------------ ------------------------------------------------------------------------- ------------------------------------------------------------------- 1 **is24HourView()** Returns true if the TimePicker is in 24-hour view mode. 2 **isEnabled()** Returns the enabled status of the TimePicker. 3 **setCurrentHour(Integer currentHour)** Sets the current hour. 4 **setCurrentMinute(Integer currentMinute)** Sets the current minute. 5 **setEnabled(boolean enabled)** Enables or disables the TimePicker. 6 **setIs24HourView(Boolean is24HourView)** Sets the mode to 24-hour or AM/PM. 7 **setOnTimeChangedListener(TimePicker.OnTimeChangedListener listener)** Sets a callback listener to detect changes in the time selection. 4. 5. **DatePicker - Methods and Descriptions** **Sr. No** **Method** **Description** ------------ ----------------------------------------------------- -------------------------------------------------------------------------- 1 **getDayOfMonth()** Gets the selected day of the month. 2 **getMonth()** Gets the selected month. 3 **getYear()** Gets the selected year. 4 **setMaxDate(long maxDate)** Sets the maximum date supported (in milliseconds since January 1, 1970). 5 **setMinDate(long minDate)** Sets the minimum date supported (in milliseconds since January 1, 1970). 6 **setSpinnersShown(boolean shown)** Shows or hides the spinners. 7 **updateDate(int year, int month, int dayOfMonth)** Updates the displayed date. 8 **getCalendarView()** Returns the calendar view. 9 **getFirstDayOfWeek()** Gets the first day of the week.