Public void insertPressed(View view) {



Download 103.36 Kb.
Page1/3
Date11.05.2021
Size103.36 Kb.
#56601
  1   2   3
MOBILEDEV

Insert Using the GUI
public void insertPressed(View view) {

   // 1. Get the values from the form fields (in the UI)
   // 2. Validate that the user has provided all the necessary information

  

   // 3. Create a new Employee object using the form data

  

   // 4. Use the DAO functions to insert the Employee into the database

  

   // 5. Show a success message

  

   // 6. Clear all the form fields and prepare to receive new data

}
 

Step 1 & 2: Getting the values from the form fields and validating


// 1. Get the values from the form fields (in the UI)

EditText etName = findViewById(R.id.etName);

EditText etDateHired = findViewById(R.id.etDateHired);
// 2. Validate that the user has provided all the necessary information

if (etName.getText().toString().isEmpty() ||

       etDateHired.toString().isEmpty() ) {

   Log.d("ABC", "Sorry, you must enter all fields.");

   // exit the function

   return;

}
Step 3: Using the form data to create a new Employe object and insert


Employee e1 = new Employee(etName.getText().toString(), etDateHired.getText().toString());
empDAO.insert(e1);
Log.d("ABC", e1.name + " was inserted.");

Expected Result:





Get all employees
public void getAllPressed(View view) {

   // get all employees

   List employeeList = empDAO.getAllEmployees();
   // output the employees to the console

   for (int i = 0; i < employeeList.size(); i++) {

       Log.d("ABC", employeeList.get(i).toString());

   }
   // @TODO:  Output all the employees to a ListView

}

Get The total number of employees
1/ Add a DAO function to query the database for the number of employees

@Dao

public interface EmployeeDAO {

   // Create a function that returns the total number of employees in the database

   // SQL --> to get the total number of employees in the employee table?

   // name = employees

   // Let me know in teh chat box

   @Query("SELECT COUNT(*) FROM employees")

   public int getNumEmployees();

}
2/ Update your Button to use your new function
public void getTotalCountPressed(View view) {

//        List employeeList = empDAO.getAllEmployees();

//        Log.d("ABC", "The number of employees is: " + employeeList.size());

       int count = empDAO.getNumEmployees();

       Log.d("ABC", "The number of employees is: " + count);
 }

Exercise:  Write a DAO function that returns the employees names (names only, no other info)


@Query("SELECT name FROM employees")

public List getEmployeeNames();

// Creat a function that returns the FIRST employee in the database

// (but only their name)

// What modification do i need to make to only return ONE employee name

@Query("SELECT name FROM employees LIMIT 1")

public String getOneName();
Performing a WHERE operation
DAO:

@Query("SELECT * FROM employees WHERE name = :empName")

public List getEmployeesByName(String empName);
MainActivty:
List employeesNamedPeter = empDAO.getEmployeesByName("Peter");

Log.d("ABC", employeesNamedPeter.toString());

Performing a where with multiple cluases




@Query("SELECT * FROM employees WHERE name = :empName")

public List getEmployeesByName(String empName);
// Example: Create a dao function that gets the hiring date of an employee with a specified id

@Query("SELECT date_hired FROM employees WHERE id = :empId")

public String getEmployeeHireDate(int empId);
@Query("SELECT * FROM employees WHERE name = :empName AND date_hired=:hireDate")

public Employee getEmployeeByNameAndHireDate(String empName, String hireDate);
------------
Example of how to use the dao functions in your MainActivity
List employeesNamedPeter = empDAO.getEmployeesByName("Peter");

Log.d("ABC", employeesNamedPeter.toString());

String hireDate = empDAO.getEmployeeHireDate(11);

Log.d("ABC", "Frank was hired on: " + hireDate);

Delete


Download 103.36 Kb.

Share with your friends:
  1   2   3




The database is protected by copyright ©ininet.org 2024
send message

    Main page