
ГУАП
КАФЕДРА № 42
ОТЧЕТ ЗАЩИЩЕН С ОЦЕНКОЙ
ПРЕПОДАВАТЕЛЬ
Доцент, канд. техн. наук |
|
|
|
В.А. Ушаков |
должность, уч. степень, звание |
|
подпись, дата |
|
инициалы, фамилия |
ОТЧЕТ О ПРАКТИЧЕСКОЙ РАБОТЕ №3 |
|
по курсу: Разработка мобильных приложений. Разработка мобильных приложений на Kotlin |
|
|
РАБОТУ ВЫПОЛНИЛ
СТУДЕНТ гр. № |
4116 |
|
|
|
|
|
|
|
подпись, дата |
|
инициалы, фамилия |
Санкт-Петербург 2024
Цель работы: выполнить проектирование и разработку мобильного приложения под ОС Android на языке программирования высокого уровня Kotlin.
Ход работы:
Вариант 10: Класс «Таксопарк»
В проект из прошлой практической работы добавлен RecyclerView, чтобы при нажатии на кнопку «Заказать такси» (Рисунок 1), открывался список выбора машины для поездки (Рисунок 2).
Рисунок 1 –Страница заказа такси
Рисунок 2- Макет RecyclerView
Для реализации списка создана модель данных элемента списка. Элемент имеет название модели машины, цену и фото (Рисунок 3).
Листинг - Модель данных элемента списка
data class ItemActivity(
val model: String,
val price: String,
val photo: Int,
val activityClass: Class<*>
)
Рисунок 3- Макет элемента списка
Для переноса данных элемента в список написан адаптер, каждый элемент представлен экземпляром ViewHolder. При нажатии на элемент списка открывается новая активность для дальнейших действия заказа такси.
Листинг – Привязка данных к представлениям списка и реализация открытия новой страницы при нажатии на элемент списка
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.model.text = item.model
holder.price.text = item.price
holder.photo.setImageResource(item.photo)
holder.itemView.setOnClickListener {
val intent = Intent(context, item.activityClass)
context.startActivity(intent)
}
}
В итоге, при нажатии на кнопку «Заказать такси» открывается страница со списком выбора машины, для удобства использования приложения добавлена кнопка «Назад» (Рисунок 4)
Рисунок 4- Список с машинами
При выборе машины открывается новая страница Заказа (Рисунок 5-6).
Рисунок 5- Нажатие на первый элемент списка
Рисунок 6 – Выбор последнего элемента списка
Вывод: в ходе выполнения практической работы дополнен проект из второй практической работы. Добавлен RecyclerView, который позволил выбрать машину для заказа такси. Определена модель данных для передачи данных в RecyclerView. Для связывания данных создан адаптер.
Приложение
Листинг AdapterActivity.kt
package com.example.myapplication
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class AdapterActivity(private val items: List<ItemActivity>, private
val context: Context) :
RecyclerView.Adapter<AdapterActivity.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val model: TextView = view.findViewById(R.id.model)
val price: TextView =
view.findViewById(R.id.price)
val photo: ImageView = view.findViewById(R.id.photo)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
ViewHolder {
val view =
LayoutInflater.from(context).inflate(R.layout.activity_item, parent,
false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.model.text = item.model
holder.price.text = item.price
holder.photo.setImageResource(item.photo)
holder.itemView.setOnClickListener {
val intent = Intent(context, item.activityClass)
context.startActivity(intent)
}
}
override fun getItemCount() = items.size
}
Листинг ItemActivity.kt
package com.example.myapplication
data class ItemActivity(
Val model: String,
Val price: String,
Val photo: Int,
val activityClass: Class<*>
)
Листинг activity_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_yellow"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp"
android:paddingBottom="5dp">
<TextView
android:id="@+id/model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/photo"
app:layout_constraintHorizontal_bias="0"
android:layout_marginEnd="8dp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_marginBottom="8dp" />
<TextView
android:id="@+id/price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="@+id/model"
app:layout_constraintTop_toBottomOf="@+id/model"
app:layout_constraintEnd_toStartOf="@+id/photo"
android:layout_marginEnd="8dp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_marginBottom="8dp" />
<ImageView
android:id="@+id/photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/car1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Листинг OrderActivity.kt
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class OrderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ordercar)
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
val items = listOf(
ItemActivity("Ferrari California", "140 р/км." , R.drawable.car1, Car1Activity::class.java),
ItemActivity("Posche 911","130 р/км.", R.drawable.car2, Car2Activity::class.java),
ItemActivity("Fiat 500","30 р/км.", R.drawable.car3, Car3Activity::class.java),
ItemActivity("Jeep Wrangler","100 р/км.", R.drawable.car4, Car4Activity::class.java),
ItemActivity("Lamborghini Huracan","150 р/км.", R.drawable.car5, Car5Activity::class.java)
)
recyclerView.adapter = AdapterActivity(items, this)
}
fun backOrder(view: View?) {
startActivity(Intent(this, OrderFragment::class.java))
}
}
Листинг activity_ordercar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_yellow"
tools:context=".OrderActivity">
<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Назад"
android:padding="1dp"
android:layout_margin="1dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:onClick="backOrder"
android:textColor="@color/light_yellow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/taxi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_yellow"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text=" Выберите машину для поездки"
android:textColor="#000000"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignParentTop="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/taxi"/>
</RelativeLayout>
Листинг Car1Activity.kt
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class Car1Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_car1)
}
fun openApp(view: View?) {
startActivity(Intent(this, Car1Activity::class.java))
}
}
Листинг activity_car1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_yellow"
tools:context=".Car1Activity">
<TextView
android:id="@+id/choise"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="15dp"
android:text="Отличный выбор! Ваша машина:"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="Ferrari California"
android:layout_below="@id/choise"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="17sp" />
<ImageView
android:id="@+id/photoCar1"
android:layout_width="401dp"
android:layout_height="280dp"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_below="@id/model"
android:src="@drawable/car1" />
<EditText
android:id="@+id/fromLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/photoCar1"
android:layout_marginTop="20dp"
android:hint="Введите адрес отправления"
android:padding="15dp" />
<EditText
android:id="@+id/toLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/fromLocationEditText"
android:layout_marginTop="10dp"
android:hint="Введите место назначения"
android:padding="15dp" />
<Button
android:id="@+id/buttonOrder"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="66dp"
android:background="@color/black"
android:layout_below="@id/fromLocationEditText"
android:text="Заказать"
android:textColor="@color/light_yellow" />
</RelativeLayout>
Листинг Car2Activity.kt
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class Car2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_car2)
}
fun openApp(view: View?) {
startActivity(Intent(this, Car2Activity::class.java))
}
}
Листинг activity_car2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_yellow"
tools:context=".Car2Activity">
<TextView
android:id="@+id/choise"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="15dp"
android:text="Отличный выбор! Ваша машина:"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="Posche 911"
android:layout_below="@id/choise"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="17sp" />
<ImageView
android:id="@+id/photoCar1"
android:layout_width="401dp"
android:layout_height="280dp"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_below="@id/model"
android:src="@drawable/car2" />
<EditText
android:id="@+id/fromLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/photoCar1"
android:layout_marginTop="20dp"
android:hint="Введите адрес отправления"
android:padding="15dp" />
<EditText
android:id="@+id/toLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/fromLocationEditText"
android:layout_marginTop="10dp"
android:hint="Введите место назначения"
android:padding="15dp" />
<Button
android:id="@+id/buttonOrder"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="66dp"
android:background="@color/black"
android:layout_below="@id/toLocationEditText"
android:text="Заказать"
android:textColor="@color/light_yellow" />
</RelativeLayout>
Листинг Car3Activity.kt
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class Car3Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_car3)
}
fun openApp(view: View?) {
startActivity(Intent(this, Car3Activity::class.java))
}
}
Листинг activity_car3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_yellow"
tools:context=".Car3Activity">
<TextView
android:id="@+id/choise"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="15dp"
android:text="Отличный выбор! Ваша машина:"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="Fiat 500"
android:layout_below="@id/choise"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="17sp" />
<ImageView
android:id="@+id/photoCar1"
android:layout_width="401dp"
android:layout_height="280dp"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_below="@id/model"
android:src="@drawable/car3" />
<EditText
android:id="@+id/fromLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/photoCar1"
android:layout_marginTop="20dp"
android:hint="Введите адрес отправления"
android:padding="15dp" />
<EditText
android:id="@+id/toLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/fromLocationEditText"
android:layout_marginTop="10dp"
android:hint="Введите место назначения"
android:padding="15dp" />
<Button
android:id="@+id/buttonOrder"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="66dp"
android:background="@color/black"
android:layout_below="@id/toLocationEditText"
android:text="Заказать"
android:textColor="@color/light_yellow" />
</RelativeLayout>
Листинг Car4Activity.kt
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class Car4Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_car4)
}
fun openApp(view: View?) {
startActivity(Intent(this, Car4Activity::class.java))
}
}
Листинг activity_car4.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_yellow"
tools:context=".Car4Activity">
<TextView
android:id="@+id/choise"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="15dp"
android:text="Отличный выбор! Ваша машина:"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="Jeep Wrangler"
android:layout_below="@id/choise"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="17sp" />
<ImageView
android:id="@+id/photoCar1"
android:layout_width="401dp"
android:layout_height="280dp"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_below="@id/model"
android:src="@drawable/car4" />
<EditText
android:id="@+id/fromLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/photoCar1"
android:layout_marginTop="20dp"
android:hint="Введите адрес отправления"
android:padding="15dp" />
<EditText
android:id="@+id/toLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/fromLocationEditText"
android:layout_marginTop="10dp"
android:hint="Введите место назначения"
android:padding="15dp" />
<Button
android:id="@+id/buttonOrder"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="66dp"
android:background="@color/black"
android:layout_below="@id/toLocationEditText"
android:text="Заказать"
android:textColor="@color/light_yellow" />
</RelativeLayout>
Листинг Car5Activity.kt
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class Car5Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_car5)
}
fun openApp(view: View?) {
startActivity(Intent(this,Car5Activity::class.java))
}
}
Листинг activity_car5.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_yellow"
tools:context=".Car5Activity">
<TextView
android:id="@+id/choise"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="15dp"
android:text="Отличный выбор! Ваша машина:"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="Lamborghini Huracan"
android:layout_below="@id/choise"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="17sp" />
<ImageView
android:id="@+id/photoCar1"
android:layout_width="401dp"
android:layout_height="280dp"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_below="@id/model"
android:src="@drawable/car5" />
<EditText
android:id="@+id/fromLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/photoCar1"
android:layout_marginTop="20dp"
android:hint="Введите адрес отправления"
android:padding="15dp" />
<EditText
android:id="@+id/toLocationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/fromLocationEditText"
android:layout_marginTop="10dp"
android:hint="Введите место назначения"
android:padding="15dp" />
<Button
android:id="@+id/buttonOrder"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="66dp"
android:background="@color/black"
android:layout_below="@id/toLocationEditText"
android:text="Заказать"
android:textColor="@color/light_yellow" />
</RelativeLayout>