From 901c9e36f58b47e9b01fc4cd591e8a5e3c431dec Mon Sep 17 00:00:00 2001 From: Iftakhar Husan <AC5636@student.jamk.fi> Date: Wed, 22 Nov 2023 06:11:53 +0200 Subject: [PATCH] Refactor --- .../MainActivity.kt | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/E03BuildUIWithLayoutEditor2/app/src/main/java/com/example/e03builduiwithlayouteditor2/MainActivity.kt b/E03BuildUIWithLayoutEditor2/app/src/main/java/com/example/e03builduiwithlayouteditor2/MainActivity.kt index 04fe1cb..094f8f4 100644 --- a/E03BuildUIWithLayoutEditor2/app/src/main/java/com/example/e03builduiwithlayouteditor2/MainActivity.kt +++ b/E03BuildUIWithLayoutEditor2/app/src/main/java/com/example/e03builduiwithlayouteditor2/MainActivity.kt @@ -1,6 +1,5 @@ package com.example.e03builduiwithlayouteditor2 -import android.graphics.Typeface import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View @@ -9,41 +8,44 @@ import android.widget.TextView class MainActivity : AppCompatActivity() { - private val firstnames = arrayOf("Renato", "Rosangela", "Tim", "Barton", "Jeannette", "Ali", "Becky", "Coco", "Ragnar") - private val lastnames = arrayOf("Ksenia", "MetLife", "Asuncion", "Zemfira", "Gang", "G", "G", "A", "B") + private val firstNames = arrayOf("Renato", "Rosangela", "Tim", "Barton", "Jeannette", "Ali", "Becky", "Coco", "Ragnar") + private val lastNames = arrayOf("Ksenia", "MetLife", "Asuncion", "Zemfira", "Gang", "G", "G", "A", "B") private val jobTitles = arrayOf("District Quality Coordinator","International Intranet Representative","District Intranet Administrator","Dynamic Research Manager","Central Infrastructure Consultant", "Lorem ipsum dolor amit sen", "Lorem ipsum dolor amit sen", "Lorem ipsum dolor amit sen", "Lorem ipsum dolor amit sen") // function displays employees data in UI private fun showEmployeeData(index: Int) { + // data for the employee + val firstName = firstNames[index] + val lastName = lastNames[index] + val jobTitle = jobTitles[index] + val employeeInfo = getString(R.string.employee_info_text, lastNames[index], firstNames[index], jobTitles[index], getString(R.string.description)) + var imageSrc = 1 + when(index) { + 0 -> imageSrc = R.drawable.employee1 + 1 -> imageSrc = R.drawable.employee2 + 2 -> imageSrc = R.drawable.employee3 + 3 -> imageSrc = R.drawable.employee4 + 4 -> imageSrc = R.drawable.employee5 + 5 -> imageSrc = R.drawable.employee6 + 6 -> imageSrc = R.drawable.employee7 + 7 -> imageSrc = R.drawable.employee8 + 8 -> imageSrc = R.drawable.employee9 + } + // find TextView's from the UI layout file val firstNameTextview = findViewById<TextView>(R.id.first_name) val lastNameTextView = findViewById<TextView>(R.id.last_name) val jobTitleTextView = findViewById<TextView>(R.id.job_title) val employeeInfoTextView = findViewById<TextView>(R.id.employee_info_text) // Update TextView texts - firstNameTextview.text = firstnames[index] - lastNameTextView.text = lastnames[index] - jobTitleTextView.text = jobTitles[index] + firstNameTextview.text = firstName + lastNameTextView.text = lastName + jobTitleTextView.text = jobTitle + employeeInfoTextView.text = employeeInfo - // info is - employeeInfoTextView.text = getString(R.string.employee_info_text, lastnames[index], firstnames[index], jobTitles[index], getString(R.string.description)) - - // image - var id = 0 - when(index) { - 0 -> id = R.drawable.employee1 - 1 -> id = R.drawable.employee2 - 2 -> id = R.drawable.employee3 - 3 -> id = R.drawable.employee4 - 4 -> id = R.drawable.employee5 - 5 -> id = R.drawable.employee6 - 6 -> id = R.drawable.employee7 - 7 -> id = R.drawable.employee8 - 8 -> id = R.drawable.employee9 - } // find imageView and display correct employee image val imageView = findViewById<ImageView>(R.id.employee_image) - imageView.setImageResource(id) + imageView.setImageResource(imageSrc) } // function will be called from the UI -- GitLab