2024.4.7

第二十三天

所花时间:2小时

代码量:400+

博客量:1

了解到的知识点:个人学习记录app——

 

<?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="match_parent"
tools:context=".teacher">

<!-- 学号输入框 -->
<EditText
android:id="@+id/editTextStudentId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="输入学号"
android:inputType="text"
app:layout_constraintEnd_toStartOf="@+id/editTextWeekNumber"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- 周数输入框 -->
<EditText
android:id="@+id/editTextWeekNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="输入周数"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextStudentId"
app:layout_constraintTop_toTopOf="parent" />

<!-- 查询按钮 -->
<Button
android:id="@+id/buttonQuery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="查询"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextWeekNumber" />

<!-- 显示信息的 ScrollView 区域 -->
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonQuery">

<TextView
android:id="@+id/textViewDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/black" />
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
 
 
 
package com.hui.demo3;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hui.demo3.dao.RecordDao;
import com.hui.demo3.dao.StudentDao;
import com.hui.demo3.entity.Record;
import com.hui.demo3.entity.StuRecData;
import com.hui.demo3.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class teacher extends AppCompatActivity {

private static final String TAG = "teacher";
private EditText editTextStudentId;
private EditText editTextWeekNumber;
private TextView textViewDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher);

// 初始化视图组件
editTextStudentId = findViewById(R.id.editTextStudentId);
editTextWeekNumber = findViewById(R.id.editTextWeekNumber);
textViewDisplay = findViewById(R.id.textViewDisplay);

// 获取查询按钮
Button buttonQuery = findViewById(R.id.buttonQuery);

// 设置查询按钮点击事件
buttonQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
queryData();
}
});
}

// 查询数据的方法
private void queryData() {
new Thread(new Runnable() {
@Override
public void run() {
if (TextUtils.isEmpty(editTextStudentId.getText()) && TextUtils.isEmpty(editTextWeekNumber.getText())) {
List<StuRecData> resultDataAll = queryAllData();
displayData("所有数据", resultDataAll);
} else if (!TextUtils.isEmpty(editTextStudentId.getText()) && TextUtils.isEmpty(editTextWeekNumber.getText())) {
String studentId = editTextStudentId.getText().toString();
List<StuRecData> resultDataById = queryByStudentId(studentId);
displayData("Data by ID", resultDataById);
} else if (TextUtils.isEmpty(editTextStudentId.getText()) && !TextUtils.isEmpty(editTextWeekNumber.getText())) {
int weekNum = Integer.parseInt(editTextWeekNumber.getText().toString());
List<StuRecData> resultDataByWeekNum = queryByWeekNum(weekNum);
displayData("Data by Week", resultDataByWeekNum);
} else {
String studentId = editTextStudentId.getText().toString();
int weekNum = Integer.parseInt(editTextWeekNumber.getText().toString());
List<StuRecData> resultDataCombined = queryByStudentIdAndWeekNum(studentId, weekNum);
displayData("Data by ID&Week", resultDataCombined);
}
}
}).start();
}


// 无参数查询,将所有数据查询出来
private List<StuRecData> queryAllData() {
List<StuRecData> resultData = new ArrayList<>();
List<Record> allRecords = new RecordDao().findAllRecords();
for (Record record : allRecords) {
String studentId = record.getStudentId();
Student student = new StudentDao().findStudent(studentId);
if (student != null) {
StuRecData stuRecData = createStuRecData(student, record);
resultData.add(stuRecData);
}
}
return resultData;
}

// 根据StudentId查询,将Record表中的对应id的打卡记录查询出来
private List<StuRecData> queryByStudentId(String studentId) {
List<StuRecData> resultData = new ArrayList<>();
List<Record> records = new RecordDao().findRecordsByStudentId(studentId);
for (Record record : records) {
Student student = new StudentDao().findStudent(studentId);
if (student != null) {
StuRecData stuRecData = createStuRecData(student, record);
resultData.add(stuRecData);
}
}
return resultData;
}

// 根据weekNum查询,将record表中,对应weekNum的信息查询出来
private List<StuRecData> queryByWeekNum(int weekNum) {
List<StuRecData> resultData = new ArrayList<>();
List<Record> records = new RecordDao().findRecordsByWeekNum(weekNum);
for (Record record : records) {
String studentId = record.getStudentId();
Student student = new StudentDao().findStudent(studentId);
if (student != null) {
StuRecData stuRecData = createStuRecData(student, record);
resultData.add(stuRecData);
}
}
return resultData;
}

// 根据两种条件同时查询
private List<StuRecData> queryByStudentIdAndWeekNum(String studentId, int weekNum) {
List<StuRecData> resultData = new ArrayList<>();
Record record = new RecordDao().findRecord(studentId, weekNum);
if (record != null) {
Student student = new StudentDao().findStudent(studentId);
if (student != null) {
StuRecData stuRecData = createStuRecData(student, record);
resultData.add(stuRecData);
}
}
return resultData;
}

// 创建 StuRecData 对象
private StuRecData createStuRecData(Student student, Record record) {
return new StuRecData(
student.getId(),
student.getName(),
student.getClassName(),
student.getSetRecord(),
record.getWeekNum(),
record.getStartTime(),
record.getEndTime(),
record.getRecording()
);
}

// 显示查询结果
// 显示查询结果
private void displayData(final String title, final List<StuRecData> resultData) {
runOnUiThread(new Runnable() {
@Override
public void run() {
StringBuilder displayText = new StringBuilder();
displayText.append(title).append(":\n");
if (!resultData.isEmpty()) {
for (StuRecData data : resultData) {
displayText.append("学生ID:").append(data.getId())
.append("\n姓名:").append(data.getName())
.append("\n班级:").append(data.getClassName())
.append("\n打卡次数:").append(data.getSetRecord())
.append("\n周数:").append(data.getWeekNum())
.append("\n开始时间:").append(data.getStartTime())
.append("\n结束时间:").append(data.getEndTime())
.append("\n学习记录:").append(data.getRecording())
.append("\n\n");
}
} else {
displayText.append("未找到相关数据\n\n");
}
textViewDisplay.setText(displayText.toString());
}
});
}

}
posted @ 2024-04-07 22:32  cvjj  阅读(18)  评论(0)    收藏  举报