个人作业第二阶段4
完成教师功能总结汇总的前端实现:
实体类:
package com.example.learningmanagement.entity
data class SummaryStatistics(
val id: Int,
val className: String,
val userId: Int,
val username: String,
val totalCount: Int
)
API服务接口:
package com.example.learningmanagement.service
import com.example.learningmanagement.entity.ApiResponse
import com.example.learningmanagement.entity.SummaryStatistics
import retrofit2.http.GET
import retrofit2.http.Path
interface SummaryStatisticsService {
@GET("/statistics/generate")
suspend fun generateStatistics(): ApiResponse<Any>
@GET("/statistics/selectAll")
suspend fun selectAll(): ApiResponse<List<SummaryStatistics>>
@GET("/statistics/selectByUserId/{userId}")
suspend fun selectByUserId(@Path("userId") userId: Int): ApiResponse<SummaryStatistics>
}
列表项布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="8dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="12dp">
<TextView
android:id="@+id/tvId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp" />
<TextView
android:id="@+id/tvClassName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="14sp" />
<TextView
android:id="@+id/tvUserId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="14sp" />
<TextView
android:id="@+id/tvUsername"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="14sp" />
<TextView
android:id="@+id/tvTotalCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 顶部标题栏 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/pink_500"
android:gravity="center_vertical"
android:paddingHorizontal="16dp">
<!-- 返回按钮 -->
<ImageButton
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/baseline_arrow_back_24"
android:tint="@color/white"
android:contentDescription="返回" />
<!-- 标题 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="学生总结统计"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />
<!-- 刷新按钮 -->
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="@+id/btnRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷新统计"
android:textSize="12sp" />
</LinearLayout>
<!-- 表头 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="12dp"
android:background="@color/gray_200">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="序号"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="班级"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="学号"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="姓名"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="总结次数"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvSummaryStatistics"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
适配器:
package com.example.learningmanagement.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.learningmanagement.R
import com.example.learningmanagement.entity.SummaryStatistics
class SummaryStatisticsAdapter : ListAdapter<SummaryStatistics, SummaryStatisticsAdapter.ViewHolder>(DiffCallback()) {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvId: TextView = view.findViewById(R.id.tvId)
val tvClassName: TextView = view.findViewById(R.id.tvClassName)
val tvUserId: TextView = view.findViewById(R.id.tvUserId)
val tvUsername: TextView = view.findViewById(R.id.tvUsername)
val tvTotalCount: TextView = view.findViewById(R.id.tvTotalCount)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_summary_statistics, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
holder.tvId.text = (position + 1).toString()
holder.tvClassName.text = item.className
holder.tvUserId.text = item.userId.toString()
holder.tvUsername.text = item.username
holder.tvTotalCount.text = item.totalCount.toString()
}
private class DiffCallback : DiffUtil.ItemCallback<SummaryStatistics>() {
override fun areItemsTheSame(oldItem: SummaryStatistics, newItem: SummaryStatistics) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: SummaryStatistics, newItem: SummaryStatistics) =
oldItem == newItem
}
}
活动类:
package com.example.learningmanagement.ui
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.learningmanagement.R
import com.example.learningmanagement.adapter.SummaryStatisticsAdapter
import com.example.learningmanagement.network.ServiceCreater
import com.example.learningmanagement.service.SummaryStatisticsService
import kotlinx.coroutines.launch
class SummaryStatisticsActivity : AppCompatActivity() {
private lateinit var adapter: SummaryStatisticsAdapter
private lateinit var rvSummaryStatistics: RecyclerView
private lateinit var btnRefresh: Button
private val statisticsService = ServiceCreater.create<SummaryStatisticsService>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_summary_statistics)
// 设置返回按钮点击事件
findViewById<ImageButton>(R.id.btnBack).setOnClickListener {
finish() // 结束当前活动,返回上一级
}
rvSummaryStatistics = findViewById(R.id.rvSummaryStatistics)
btnRefresh = findViewById(R.id.btnRefresh)
setupRecyclerView()
loadStatistics()
btnRefresh.setOnClickListener {
generateStatistics()
}
}
private fun setupRecyclerView() {
adapter = SummaryStatisticsAdapter()
rvSummaryStatistics.apply {
layoutManager = LinearLayoutManager(this@SummaryStatisticsActivity)
adapter = this@SummaryStatisticsActivity.adapter
}
}
private fun loadStatistics() {
lifecycleScope.launch {
try {
val response = statisticsService.selectAll()
if (response.code == "200") {
response.data?.let { statistics ->
adapter.submitList(statistics)
}
} else {
Toast.makeText(this@SummaryStatisticsActivity, "获取统计数据失败", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Toast.makeText(this@SummaryStatisticsActivity, "网络错误: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}
private fun generateStatistics() {
lifecycleScope.launch {
try {
val response = statisticsService.generateStatistics()
if (response.code == "200") {
Toast.makeText(this@SummaryStatisticsActivity, "统计数据已更新", Toast.LENGTH_SHORT).show()
loadStatistics() // 重新加载数据
} else {
Toast.makeText(this@SummaryStatisticsActivity, "更新统计数据失败", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Toast.makeText(this@SummaryStatisticsActivity, "网络错误: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}
}

浙公网安备 33010602011771号