5.22

继续与小组成员讨论如何完善每日心情的记录并且统计出来,根据不同的统计内容进行分析

代码行量:166行

学习所花时间:0.5h

 

 

package com.example.memosystem.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.example.memosystem.R;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class qingxuquxian extends AppCompatActivity {

private ListView listViewData;
private TextView textViewStatistics;
private TextView textViewRecentScores;
private List<String> dataList = new ArrayList<>();
private ArrayAdapter<String> adapter;

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

listViewData = findViewById(R.id.listViewData);
textViewStatistics = findViewById(R.id.textViewStatistics);
textViewRecentScores = findViewById(R.id.textViewRecentScores);

// 设置适配器
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
listViewData.setAdapter(adapter);

// 异步获取数据
new FetchDataTask().execute();
}

private class FetchDataTask extends AsyncTask<Void, Void, List<String>> {

@Override
protected List<String> doInBackground(Void... voids) {
String url = "jdbc:mysql://10.99.118.55:3306/xinli";
String user = "root";
String password = "root";

List<String> tempDataList = new ArrayList<>();
List<Integer> recentFen = new ArrayList<>();
List<String> dates = new ArrayList<>();

try (Connection connection = DriverManager.getConnection(url, user, password)) {
String query = "SELECT date, fen FROM meiri";
PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
String date = resultSet.getString("date");
int fen = resultSet.getInt("fen");
tempDataList.add("日期: " + date + ", 分数: " + fen);

recentFen.add(fen);
dates.add(date);
}

// 取最近10天的情绪分数
int startIndex = Math.max(0, recentFen.size() - 10);
List<Integer> lastTenDaysFen = recentFen.subList(startIndex, recentFen.size());
List<String> lastTenDaysDates = dates.subList(startIndex, dates.size());

// 判断情绪是否稳定
if (isStable(lastTenDaysFen)) {
tempDataList.add("最近情绪状态显示出显著的稳定性,分数波动范围极小。这种持续的情绪平稳状态反映了良好的情绪调节能力,能够有效应对日常压力和情感波动,保持内在的情绪平衡。");
} else {
tempDataList.add("最近情绪波动较大,缺乏稳定性,可能反映了情绪调节能力的不足。此种情况下,情绪可能受到外界刺激较大程度的影响,导致情绪波动频繁且剧烈,难以保持稳定的心理状态。情绪调节能力不足可能会影响个体的日常生活和工作效率,需要加强情绪调节训练以提高情绪稳定性。");
}

// 统计每个分数出现的天数
Map<Integer, Integer> fenCountMap = new HashMap<>();
for (int fen : lastTenDaysFen) {
fenCountMap.put(fen, fenCountMap.getOrDefault(fen, 0) + 1);
}

// 查找起伏较大的两天
List<Integer> changes = new ArrayList<>();
for (int i = 1; i < lastTenDaysFen.size(); i++) {
changes.add(Math.abs(lastTenDaysFen.get(i) - lastTenDaysFen.get(i - 1)));
}

int maxChangeIndex1 = changes.indexOf(Collections.max(changes));
int maxChangeIndex2 = maxChangeIndex1 == 0 ? 1 : 0;
for (int i = 0; i < changes.size(); i++) {
if (i != maxChangeIndex1 && changes.get(i) > changes.get(maxChangeIndex2)) {
maxChangeIndex2 = i;
}
}

String statistics = "最近10天每个分数出现的天数:\n";
for (Map.Entry<Integer, Integer> entry : fenCountMap.entrySet()) {
statistics += "分数 " + entry.getKey() + ": " + entry.getValue() + " 天\n";
}

statistics += "\n起伏较大的两天:\n";
statistics += lastTenDaysDates.get(maxChangeIndex1) + " 和 " + lastTenDaysDates.get(maxChangeIndex1 + 1) + "\n";
statistics += lastTenDaysDates.get(maxChangeIndex2) + " 和 " + lastTenDaysDates.get(maxChangeIndex2 + 1);

// 更新TextView用于显示最近10天的分数
StringBuilder recentScores = new StringBuilder("最近10天的分数:\n");
for (int i = 0; i < lastTenDaysDates.size(); i++) {
recentScores.append(lastTenDaysDates.get(i)).append(": ").append(lastTenDaysFen.get(i)).append("\n");
}

// Update the TextViews with the statistics and recent scores
final String finalStatistics = statistics;
final String finalRecentScores = recentScores.toString();
runOnUiThread(() -> {
textViewStatistics.setText(finalStatistics);
textViewRecentScores.setText(finalRecentScores);
});

} catch (SQLException e) {
e.printStackTrace();
tempDataList.add("获取数据时出错");
}

return tempDataList;
}

@Override
protected void onPostExecute(List<String> result) {
dataList.clear();
dataList.addAll(result);
adapter.notifyDataSetChanged();
}

private boolean isStable(List<Integer> recentFen) {
if (recentFen.isEmpty()) {
return false;
}

int startIndex = Math.max(0, recentFen.size() - 10);
List<Integer> lastTenDaysFen = recentFen.subList(startIndex, recentFen.size());

double sum = 0;
for (int fen : lastTenDaysFen) {
sum += fen;
}
double average = sum / lastTenDaysFen.size();
double variance = 0;
for (int fen : lastTenDaysFen) {
variance += Math.pow(fen - average, 2);
}
double standardDeviation = Math.sqrt(variance / lastTenDaysFen.size());

return standardDeviation <= 1;
}
}
}
posted @ 2024-06-11 09:09  不如喝点  阅读(6)  评论(0编辑  收藏  举报