2024.3.18(周一)进度

完成连接远程数据库的增,完成了2个文件,代码量为200行。博客园数量:一篇

package com.example.learningrecords;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.learningrecords.R;
import com.example.learningrecords.dao.StudentDao;

public class MainActivity extends AppCompatActivity {

private Button signUpButton;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signUpButton = findViewById(R.id.SignUpButton);

signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 创建一个意图对象,用于启动注册页面的活动
Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
// 启动注册页面的活动
startActivity(intent);
}
});
}


/**
* function: 登录
* */
public void login(View view){

EditText EditTextAccount = findViewById(R.id.UserNameEdit);
EditText EditTextPassword = findViewById(R.id.PassWordEdit);
String account = EditTextAccount.getText().toString();
String password = EditTextPassword.getText().toString();


// 检查是否为教师账号,如果是则直接跳转到教师界面
if (account.equals("teacher") && password.equals("teacher")) {
Intent intent = new Intent(MainActivity.this, teacher.class);
startActivity(intent);
} else {
new Thread() {
@Override
public void run() {
StudentDao studentDao = new StudentDao();
int msg = studentDao.login(EditTextAccount.getText().toString(), EditTextPassword.getText().toString());
hand1.sendEmptyMessage(msg);
}
}.start();
}

}

@SuppressLint("HandlerLeak")
final Handler hand1 = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
// 登录成功后启动新的活动
startActivity(new Intent(MainActivity.this, MainActivity2.class));
} else if (msg.what == 0) {
Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
} else if (msg.what == 2) {
Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
} else if (msg.what == 3) {
Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
}
}
};

}


package com.example.learningrecords;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

import com.example.learningrecords.R;
import com.example.learningrecords.dao.PlanningDao;
import com.example.learningrecords.dao.StudentDao;
import com.example.learningrecords.entity.StatisticData;
import com.example.learningrecords.entity.Student;

import java.util.ArrayList;


public class MainActivity2 extends AppCompatActivity {

private Button goalAnalysisButton;
private Button dailyCheckInButton;
private Button statisticsButton;

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

goalAnalysisButton = findViewById(R.id.GoalAnalysisButton);
dailyCheckInButton = findViewById(R.id.DailyCheckInButton);
statisticsButton = findViewById(R.id.StatisticsButton);

// 目标与分析按钮点击事件
goalAnalysisButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, GoalAnalysis.class);
startActivity(intent);
}
});

// 每日打卡按钮点击事件
dailyCheckInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, DailyCheckIn.class);
startActivity(intent);
}
});
}


// 处理统计与分析按钮点击事件
public void queryStatisticsData(View view) {
new Thread() {
@Override
public void run() {
// 在后台线程中执行查询统计数据的操作

// 获取当前登录用户的 studentId
String studentId = StudentDao.getCurrentLoggedInUserId();

// 获取学生对象
StudentDao studentDao = new StudentDao();
Student student = studentDao.findStudent(studentId);
if (student != null) {
// 获取 setGoal、setRecord 和完成度百分比
int setGoal = student.getSetGoal();
int setRecord = student.getSetRecord();
double completionPercentage = PlanningDao.calculateCompletionPercentage(studentId);

// 创建一个列表存储数据
ArrayList<StatisticData> statisticDataList = new ArrayList<>();
statisticDataList.add(new StatisticData(studentId, setGoal, setRecord, completionPercentage));

// 将数据传递给 Statistics 类(com.example.demo3 包下)
Intent intent = new Intent(MainActivity2.this, Statistics.class);
intent.putParcelableArrayListExtra("statisticDataList", statisticDataList);
startActivity(intent);
}
}
}.start();
}

}


posted @ 2024-03-19 19:30  记得关月亮  阅读(4)  评论(0编辑  收藏  举报