2024/03/29(2024春季)

学习时长:2小时

代码行数:138行

博客数量: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=".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:id="@+id/s"
        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:id="@+id/e"
        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:id="@+id/m"
        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_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

    <TextView
        android:id="@+id/empty1"
        android:visibility="invisible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/empty"
        android:textSize="40sp" />

</LinearLayout>
package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.adapter.RecordAdapter;
import com.example.myapplication.dphelper.GoalsDbHelper;
import com.example.myapplication.dphelper.RecordDbHelper;
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.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class RecordListActivity extends AppCompatActivity {

    private TextView empty;
    private ListView listView;
    private List<Record> list;
    private String username;
//    private RecordDbHelper recordDbHelper;
//    private GoalsDbHelper goalsDbHelper;
    private SharedPreferences preferences;

//    @Override
//    protected void onStart() {
//        super.onStart();
//        recordDbHelper = RecordDbHelper.getInstance(this);
//        goalsDbHelper = GoalsDbHelper.getInstance(this);
//        recordDbHelper.openReadLink();
//        recordDbHelper.openWriteLink();
//        goalsDbHelper.openReadLink();
//        goalsDbHelper.openWriteLink();
//        LocalDate localDate = LocalDate.now();
//        list = recordDbHelper.select(username, localDate);
//
//        if (!list.isEmpty()) {
//            System.out.println(list + "11111");
//            RecordAdapter recordAdapter = new RecordAdapter(list, this);
//            listView.setAdapter(recordAdapter);
//        }
//        else {
//            empty.setVisibility(View.VISIBLE);
//        }
//
//    }
//
//    @Override
//    protected void onStop() {
//        recordDbHelper.close();
//        goalsDbHelper.close();
//        super.onStop();
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_recordlist);
        init();
        list=new ArrayList<>();
        preferences = getSharedPreferences("user2", Context.MODE_PRIVATE);
        username = preferences.getString("username", null);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Connection connection=null;
                connection= JDBCUtils.getConn();
                LocalDate today=LocalDate.now();
                LocalDate start_Date = today.with(DayOfWeek.MONDAY);
                LocalDate end_Date = today.with(DayOfWeek.SUNDAY).plusDays(1);
                String sql="select * from record where username=? and start_time between ? and ?";
                try {
                    PreparedStatement ps=connection.prepareStatement(sql);
                    ps.setString(1,username);
                    ps.setString(2, String.valueOf(start_Date));
                    ps.setString(3, String.valueOf(end_Date));
                    ResultSet resultSet= ps.executeQuery();
                    while (resultSet.next())
                    {
                        Record record=new Record();
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        record.setId(resultSet.getInt(1));
                        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));
                        list.add(record);
                    }
                    RecordListActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!list.isEmpty()) {
                                System.out.println(list + "11111");
                                RecordAdapter recordAdapter = new RecordAdapter(list, RecordListActivity.this);
                                listView.setAdapter(recordAdapter);
                            }
                            else {
                                empty.setVisibility(View.VISIBLE);
                            }
                        }
                    });
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    private void init() {
        empty=findViewById(R.id.empty1);
        listView=findViewById(R.id.list_view);
    }
}

 

posted @ 2024-03-29 22:21  伐木工熊大  阅读(3)  评论(0编辑  收藏  举报