继续与小组成员讨论如何完善每日心情的记录
代码行量:160行
学习所花时间:1h
package com.example.memosystem.activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
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.SQLException;
public class meirixinqing extends AppCompatActivity {
private ImageView moodImageView;
private EditText dateEditText;
private Button changeImageButton;
private Button saveButton;
private int selectedMood = 1; // 初始选择第一张图片
private static final String TAG = "mysql11111";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meirixinqing);
moodImageView = findViewById(R.id.moodImageView);
dateEditText = findViewById(R.id.dateEditText);
changeImageButton = findViewById(R.id.changeImageButton);
saveButton = findViewById(R.id.saveButton);
changeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 切换图片逻辑
selectedMood = (selectedMood % 5) + 1; // 循环切换 1-5
updateMoodImage(selectedMood);
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 保存心情分数到数据库
saveMoodToDatabase(selectedMood);
}
});
// 初始显示第一张图片
updateMoodImage(selectedMood);
}
// 更新显示的心情图片
private void updateMoodImage(int moodNumber) {
switch (moodNumber) {
case 1:
moodImageView.setImageResource(R.drawable.flower1);
break;
case 2:
moodImageView.setImageResource(R.drawable.flower2);
break;
case 3:
moodImageView.setImageResource(R.drawable.flower3);
break;
case 4:
moodImageView.setImageResource(R.drawable.flower4);
break;
case 5:
moodImageView.setImageResource(R.drawable.flower5);
break;
}
}
// 保存心情分数到数据库
private void saveMoodToDatabase(int moodNumber) {
String inputDate = dateEditText.getText().toString();
final Thread thread =new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(100); // 每隔0.1秒尝试连接
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
}
// 1.加载JDBC驱动
try {
Class.forName("com.mysql.jdbc.Driver");
Log.v(TAG, "加载JDBC驱动成功");
} catch (ClassNotFoundException e) {
Log.e(TAG, "加载JDBC驱动失败");
return;
}
// 2.设置好IP/端口/数据库名/用户名/密码等必要的连接信息
String ip = "10.99.118.55";
int port = 3306;
String dbName = "xinli";
String url = "jdbc:mysql://" + ip + ":" + port
+ "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
// 构建连接mysql的字符串
String user = "root";
String password = "root";
// 3.连接JDBC
try {
java.sql.Connection conn = DriverManager.getConnection(url, user, password);
Log.d(TAG, "数据库连接成功");
String sql="insert into meiri (date,fen) values (?,?)";
PreparedStatement pstm= conn.prepareStatement(sql);
pstm.setString(1, inputDate);
pstm.setInt(2, getScoreFromMoodNumber(moodNumber));
pstm.executeUpdate();
// 添加保存成功提示
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(meirixinqing.this, "保存成功", Toast.LENGTH_SHORT).show();
}
});
return;
}
catch (SQLException e) {
Log.e(TAG, e.getMessage());
Toast.makeText(meirixinqing.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
}
});
thread.start();
}
// 根据选择的心情图片返回分数
private int getScoreFromMoodNumber(int moodNumber) {
switch (moodNumber) {
case 1:
return 5;
case 2:
return 1;
case 3:
return 2;
case 4:
return 3;
case 5:
return 4;
default:
return 0;
}
}
} 标签:case,return,String,private,5.15,import,moodImageView From: https://www.cnblogs.com/zljzy/p/18241487