4.1

public class GoalAnalysis extends AppCompatActivity {

private EditText weekNumberEditText;
private EditText goalEditText;
private EditText analysisEditText;
private EditText goalCompletionEditText;
private Button setGoalButton;
private Button analyzeGoalCompletionButton;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

// 初始化界面组件
weekNumberEditText = findViewById(R.id.weekNumberEditText);
goalEditText = findViewById(R.id.goalEditText);
analysisEditText = findViewById(R.id.analysisEditText);
goalCompletionEditText = findViewById(R.id.goalCompletionEditText);
setGoalButton = findViewById(R.id.setGoalButton);
analyzeGoalCompletionButton = findViewById(R.id.analyzeGoalCompletionButton);

}

// 方法:插入目标数据
public void insertGoal(View view) {

String weekNum = weekNumberEditText.getText().toString();
String goal = goalEditText.getText().toString();

// 创建 Planning 对象
Planning planning = new Planning();

planning.setWeekNum(Integer.parseInt(weekNum));
planning.setGoal(goal);

new Thread() {
@Override
public void run() {
int msg = 0;
PlanningDao planningDao = new PlanningDao();
boolean flag = planningDao.insertGoal(planning);
if (flag) {
msg = 2;
// 在目标插入成功时调用 StudentDao 的 incrementSetGoal() 方法增加 setGoal 字段的值
StudentDao.incrementSetGoal();
}
hand.sendEmptyMessage(msg);
}
}.start();
}
@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 == 2) {
Toast.makeText(getApplicationContext(), "目标录入成功", Toast.LENGTH_LONG).show();
finish();
}
}
};

// 方法:插入目标分析数据
public void insertAnalysis(View view) {
String weekNum = weekNumberEditText.getText().toString();
String analysis = analysisEditText.getText().toString();
String completion = goalCompletionEditText.getText().toString();

// 创建 Planning 对象
Planning planning = new Planning();
planning.setWeekNum(Integer.parseInt(weekNum));
planning.setAnalysis(analysis);
planning.setComplete(Integer.parseInt(completion));

new Thread() {
@Override
public void run() {
PlanningDao planningDao = new PlanningDao();
boolean success = planningDao.insertAnalysis(planning);
if (success) {
hand1.sendEmptyMessage(1); // 成功消息
} else {
hand1.sendEmptyMessage(0); // 失败消息
}
}
}.start();
}

// Handler 处理消息
@SuppressLint("HandlerLeak")
final Handler hand1 = 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-04-01 19:24  涨涨涨张  阅读(6)  评论(0编辑  收藏  举报