每日打卡app

今天在b站看了一些课,听的时候都会,自己写的时候全忘。

这是一个打卡app的插入数据代码


public class DailyCheckIn extends AppCompatActivity {

private EditText startTimeEditText;
private EditText endTimeEditText;
private EditText weekNumberEditText;
private EditText studyRecordEditText;
private Button submitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_check_in);

// 初始化界面组件
startTimeEditText = findViewById(R.id.startTimeEditText);
endTimeEditText = findViewById(R.id.endTimeEditText);
weekNumberEditText = findViewById(R.id.weekNumberEditText);
studyRecordEditText = findViewById(R.id.studyRecordEditText);
submitButton = findViewById(R.id.submitButton);

}

// 方法:插入每日打卡数据
public void insertDailyCheckIn(View view) {

String startTime = startTimeEditText.getText().toString();
String endTime = endTimeEditText.getText().toString();
String weekNumber = weekNumberEditText.getText().toString();
String studyRecord = studyRecordEditText.getText().toString();

// 创建 Record 对象
Record record = new Record();
record.setStudentId(getCurrentLoggedInUserId());
record.setStartTime(startTime);
record.setEndTime(endTime);
record.setWeekNum(Integer.parseInt(weekNumber));
record.setRecording(studyRecord);

new Thread() {
@Override
public void run() {
RecordDao recordDao = new RecordDao();
boolean success = recordDao.insertRecord(record);
if (success) {
hand.sendEmptyMessage(1); // 插入成功消息
StudentDao.incrementSetRecord();
} else {
hand.sendEmptyMessage(0); // 插入失败消息
}
}
}.start();
}

// Handler 处理消息
@SuppressLint("HandlerLeak")
final Handler hand = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0) {
Toast.makeText(getApplicationContext(), "每日打卡数据录入失败", Toast.LENGTH_LONG).show();
} else if (msg.what == 1) {
Toast.makeText(getApplicationContext(), "每日打卡数据录入成功", Toast.LENGTH_LONG).show();
finish(); // 插入成功后关闭当前活动
}
}
};
}

posted @ 2024-03-29 20:30  涨涨涨张  阅读(3)  评论(0编辑  收藏  举报