2024/04/06(2024春季)
学习时长:4小时
代码行数:300+
博客数量:1篇
今天继续完成个人作业的教师端,查看学生打卡记录,教师使用该功能可以看到学生的名单,点击对应名字可以查看对应的打卡记录。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivityTotal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/name" android:gravity="center" android:textSize="40sp"> </TextView> <ListView android:id="@+id/listname" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout>
package com.example.myapplication; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.myapplication.pojo.User; import com.example.myapplication.utils.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class MainActivityTotal extends AppCompatActivity { private List<User> userlist; private ListView name; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_total); init(); name.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(MainActivityTotal.this,MainActivityPersonRecord.class); intent.putExtra("username",userlist.get(position).getUsername()); Log.i("username",userlist.get(position).getUsername()); startActivity(intent); } }); } private void init() { userlist=new ArrayList<>(); name=findViewById(R.id.listname); // List<User> tempUser=new ArrayList<>(); new Thread(new Runnable() { @Override public void run() { Connection coon = null; coon = JDBCUtils.getConn(); String sql="select * from user"; try { PreparedStatement preparedStatement=coon.prepareStatement(sql); ResultSet resultSet=preparedStatement.executeQuery(); while (resultSet.next()) { User user=new User(); user.setUsername(resultSet.getString(2)); user.setName(resultSet.getString(3)); userlist.add(user); } } catch (SQLException e) { throw new RuntimeException(e); } MainActivityTotal.this.runOnUiThread(new Runnable() { @Override public void run() { List<String> namelist=new ArrayList<>(); for(int i=0;i<userlist.size();i++) { namelist.add(userlist.get(i).getName()); } adapter=new ArrayAdapter<String>(MainActivityTotal.this,R.layout.namelist,namelist); name.setAdapter(adapter); } }); } }).start(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".RecordListActivity" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/record" android:textSize="40sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40sp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:gravity="center" android:textSize="25sp" android:text="@string/st" android:layout_height="wrap_content" android:layout_weight="5"/> <Space android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="0dp" android:gravity="center" android:textSize="25sp" android:text="@string/et" android:layout_height="wrap_content" android:layout_weight="5"/> <Space android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="0dp" android:gravity="center" android:textSize="25sp" android:text="@string/record" android:layout_height="wrap_content" android:layout_weight="5"/> </LinearLayout> <ListView android:id="@+id/list_record" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> <TextView android:id="@+id/empty_r" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="" android:textSize="40sp" /> </LinearLayout>
package com.example.myapplication; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.myapplication.adapter.RecordAdapter; import com.example.myapplication.pojo.Record; import com.example.myapplication.utils.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; public class MainActivityPersonRecord extends AppCompatActivity { private TextView empty_v; private ListView list_record; private List<Record> records; private String username; private RecordAdapter recordAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_person_record); init(); } private void init() { empty_v=findViewById(R.id.empty_r); list_record=findViewById(R.id.list_record); records=new ArrayList<>(); String username=getIntent().getStringExtra("username"); new Thread(new Runnable() { @Override public void run() { Connection coon=null; coon= JDBCUtils.getConn(); String sql="select * from record where username=?"; try { PreparedStatement ps=coon.prepareStatement(sql); ps.setString(1,username); ResultSet resultSet=ps.executeQuery(); while (resultSet.next()) { Record record =new Record(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); record.setUsername(resultSet.getString(2)); record.setStart_time(LocalDateTime.parse(resultSet.getString(3).substring(0,19),formatter)); record.setEnd_time(LocalDateTime.parse(resultSet.getString(4).substring(0,19),formatter)); record.setMessage(resultSet.getString(6)); records.add(record); } MainActivityPersonRecord.this.runOnUiThread(new Runnable() { @Override public void run() { if(records.isEmpty()) { Log.i("kong","空"); empty_v.setText("该学生无打卡记录"); }else { recordAdapter=new RecordAdapter(records,MainActivityPersonRecord.this); list_record.setAdapter(recordAdapter); } } }); } catch (SQLException e) { throw new RuntimeException(e); } } }).start(); } }