2024/04/18(2024春季)

学习时长:2小时

代码行数:200行左右

博客数量: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"
    tools:context=".MainActivityEveryday"
    android:orientation="vertical">
    <Space
        android:layout_width="match_parent"
        android:layout_height="20sp" />
    <TextView
        android:gravity="center"
        android:text="@string/querydate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40sp"/>
    <Space
        android:layout_width="match_parent"
        android:layout_height="20sp" />
    <DatePicker
        android:id="@+id/date_picker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:datePickerMode="spinner"
        android:calendarViewShown="false">
    </DatePicker>
    <Space
        android:layout_width="match_parent"
        android:layout_height="20sp" />
    <TextView
        android:gravity="center"
        android:text="@string/querykey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40sp"/>

    <Space
        android:layout_width="match_parent"
        android:layout_height="20sp" />
    <EditText
        android:id="@+id/querykey"
        android:layout_width="match_parent"
        android:layout_height="60sp"
        android:hint="@string/hint">
    </EditText>
    <Space
        android:layout_width="match_parent"
        android:layout_height="50sp" />
    <Button
        android:id="@+id/query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/query"
        android:textSize="30sp"/>
    <LinearLayout
        android:visibility="gone"
        android:id="@+id/query_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:textSize="25sp"
                android:text="@string/name"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="4">
            </TextView>
            <Space
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
            <TextView
                android:textSize="25sp"
                android:text="@string/st"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="14">
            </TextView>
            <Space
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
            <TextView
                android:textSize="25sp"
                android:text="@string/et"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="14">
            </TextView>
            <Space
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
            <TextView
                android:textSize="25sp"
                android:text="@string/record"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="6">
            </TextView>
        </LinearLayout>
        <ListView
        android:id="@+id/query_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        </ListView>
    </LinearLayout>
</LinearLayout>
package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.adapter.QueryResultAdapter;
import com.example.myapplication.pojo.QueryResult;
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.Collection;
import java.util.List;

public class MainActivityEveryday extends AppCompatActivity {
    private DatePicker datePicker;
    private EditText q_key;
    private Button query;
    private LinearLayout linearLayout;
    private ListView result_list;
    private List<QueryResult> queryResults;
    private QueryResultAdapter queryResultAdapter;

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


        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        queryResults.clear();
                        int day=datePicker.getDayOfMonth();
                        int month=datePicker.getMonth()+1;
                        int year=datePicker.getYear();
                        String date=year+"-";
                        if(month>0&&month<10)
                        {
                            date+="0"+month+"-";
                        }
                        else date+=(month+"-");
                        if(day>0&&day<10)
                        {
                            date+="0"+day;
                        }
                        else {
                            date+=day;
                        }
                        String key=q_key.getText().toString();


                        Connection connection=null;
                        connection=JDBCUtils.getConn();
                        String sql=null;
                        if (key.isEmpty())
                        {
                            sql="select name,start_time,end_time,message,user.username from record,user where user.username=record.username and record.start_time like ?";
                            try {
                                PreparedStatement ps = connection.prepareStatement(sql);
                                ps.setString(1, date + "%");
                                ResultSet resultSet = ps.executeQuery();
                                while (resultSet.next()) {
                                    QueryResult queryResult = new QueryResult();
                                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                                    queryResult.setName(resultSet.getString(1));
                                    queryResult.setStart_time(LocalDateTime.parse(resultSet.getString(2).substring(0, 19), formatter));
                                    queryResult.setEnd_time(LocalDateTime.parse(resultSet.getString(3).substring(0, 19), formatter));
                                    queryResult.setMessage(resultSet.getString(4));
                                    queryResult.setUsername(resultSet.getString(5));
                                    Log.e("result", queryResult.toString());
                                    queryResults.add(queryResult);
                                }
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }
                        }else {
                            sql = "select name,start_time,end_time,message,user.username from record,user where user.username=record.username and record.message like ? and record.start_time like ?";
                            try {
                                PreparedStatement ps = connection.prepareStatement(sql);
                                Log.e("result", "1");
                                ps.setString(1, "%" + key + "%");
                                Log.e("result", "2");
                                ps.setString(2, date + "%");
                                ResultSet resultSet = ps.executeQuery();
                                while (resultSet.next()) {
                                    QueryResult queryResult = new QueryResult();
                                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                                    queryResult.setName(resultSet.getString(1));
                                    queryResult.setStart_time(LocalDateTime.parse(resultSet.getString(2).substring(0, 19), formatter));
                                    queryResult.setEnd_time(LocalDateTime.parse(resultSet.getString(3).substring(0, 19), formatter));
                                    queryResult.setMessage(resultSet.getString(4));
                                    queryResult.setUsername(resultSet.getString(5));
                                    Log.e("result", queryResult.toString());
                                    queryResults.add(queryResult);
                                }
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        MainActivityEveryday.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.i("dhakldha","dada");
                                linearLayout.setVisibility(View.VISIBLE);
                                queryResultAdapter=new QueryResultAdapter(queryResults,MainActivityEveryday.this);
                                result_list.setAdapter(queryResultAdapter);
                            }
                        });
                    }
                }).start();
            }
        });
    }
    private void init(){
        datePicker =findViewById(R.id.date_picker);
        q_key=findViewById(R.id.querykey);
        query=findViewById(R.id.query);
        linearLayout=findViewById(R.id.query_view);
        result_list=findViewById(R.id.query_list);
        queryResults=new ArrayList<>();
    }
}

 

posted @ 2024-04-18 22:42  伐木工熊大  阅读(4)  评论(0编辑  收藏  举报