6.5

public class ViewRecordsActivity extends AppCompatActivity {

private ExecutorService executorService = Executors.newSingleThreadExecutor();
private ListView listViewRecords;
private List<MoodRecord> moodRecordList;
private MoodRecordAdapter moodRecordAdapter;

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

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("View Records");

toolbar.setNavigationOnClickListener(view -> finish());

listViewRecords = findViewById(R.id.listView_records);
moodRecordList = new ArrayList<>();
moodRecordAdapter = new MoodRecordAdapter(this, moodRecordList);
listViewRecords.setAdapter(moodRecordAdapter);

fetchRecordsFromDatabase();

listViewRecords.setOnItemClickListener((parent, view, position, id) -> {
MoodRecord record = moodRecordList.get(position);
Intent intent = new Intent(ViewRecordsActivity.this, RecordDetailActivity.class);
intent.putExtra("age", record.getAge());
intent.putExtra("beauty", record.getBeauty());
intent.putExtra("expression", record.getExpression());
intent.putExtra("image", record.getImageBase64());
intent.putExtra("mood", record.getMood());
intent.putExtra("date", record.getDate());
startActivity(intent);
});
}

private void fetchRecordsFromDatabase() {
executorService.execute(() -> {
try {
Connection connection = JDBCUtils.getConn();
if (connection != null) {
String sql = "SELECT age, beauty, expression, image, mood, DATE_FORMAT(date, '%Y-%m-%d %H:%i:%s') as date FROM mood_records";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String age = resultSet.getString("age");
String beauty = resultSet.getString("beauty");
String expression = resultSet.getString("expression");
String image = resultSet.getString("image");
String mood = resultSet.getString("mood");
String date = resultSet.getString("date");
MoodRecord record = new MoodRecord(age, beauty, expression, image, mood, date);
moodRecordList.add(record);
}
runOnUiThread(() -> moodRecordAdapter.notifyDataSetChanged());
resultSet.close();
preparedStatement.close();
connection.close();
}
} catch (SQLException e) {
Log.e("ViewRecordsActivity", "SQL Exception: " + e.getMessage());
}
});
}
}
posted @ 2024-06-05 20:01  涨涨涨张  阅读(3)  评论(0编辑  收藏  举报