学习进度条

今日学习时间:3 小时
今日代码量:300 行
今日博客:1 篇

一、核心操作

查看打卡记录

在ViewRecordsActivity.java中,用户可以查看编程打卡记录。通过Retrofit调用后端 API 获取记录数据,并使用RecyclerView进行展示。

ApiService apiService = RetrofitClient.getClient().create(ApiService.class);
Call<List<ProgrammingRecord>> call = apiService.getProgrammingRecords(token);
call.enqueue(new Callback<List<ProgrammingRecord>>() {
    @Override
    public void onResponse(Call<List<ProgrammingRecord>> call, Response<List<ProgrammingRecord>> response) {
        if (response.isSuccessful() && response.body() != null) {
            adapter.setRecords(response.body());
        } else {
            Toast.makeText(ViewRecordsActivity.this, "获取记录失败: " + response.message(), Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFailure(Call<List<ProgrammingRecord>> call, Throwable t) {
        Toast.makeText(ViewRecordsActivity.this, "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

数据导出

在TeacherMainActivity.java中,教师可以将打卡记录导出为 CSV 或 Excel 格式。

private void exportToCSV() {
    if (adapter.getItemCount() == 0) {
        Toast.makeText(this, "没有数据可导出", Toast.LENGTH_SHORT).show();
        return;
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
    String fileName = "学生打卡记录_" + sdf.format(new Date()) + ".csv";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        File dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
        File file = new File(dir, fileName);
        writeCSVFile(file);
    } else {
        File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File file = new File(downloadsDir, fileName);
        writeCSVFile(file);
    }
}
private void writeCSVFile(File file) {
    try {
        FileWriter writer = new FileWriter(file);
        writer.append("学号,日期,PSP阶段,耗时(分钟),内容\n");
        for (ProgrammingRecord record : adapter.getRecords()) {
            writer.append(String.format("%s,%s,%s,%d,%s\n",
                record.getUserId(),
                record.getRecordDate(),
                record.getPspStage(),
                record.getTimeSpent(),
                record.getContent() != null ? record.getContent().replace(",", ",") : ""));
        }
        writer.flush();
        writer.close();
        Toast.makeText(this, "数据已导出到: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "导出失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

在AndroidManifest.xml中添加文件读写权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

三、安全注意事项

权限申请:

在导出数据时,需要动态申请文件读写权限,确保应用在不同 Android 版本上的兼容性。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    showExportOptions();
} else {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            REQUEST_WRITE_EXTERNAL_STORAGE);
    } else {
        showExportOptions();
    }
}

数据安全:

在导出数据时,确保数据的完整性和保密性,避免敏感信息的泄露。

四、今日实践成果

实现了查看打卡记录的功能,使用RecyclerView展示数据。
完成了数据导出为 CSV 格式的功能,支持不同 Android 版本的文件存储。

遇到的问题:

权限申请失败(解决:检查权限声明和动态申请逻辑)
数据导出格式错误(解决:检查 CSV 文件写入逻辑)

五、明日计划

学习查看每日总结的功能实现。
研究如何优化数据导出功能,支持更多格式。

六、收获

掌握了RecyclerView的使用,实现了数据的列表展示。
了解了文件读写权限的申请和数据导出的基本流程。

posted @ 2025-05-03 15:26  haoyinuo  阅读(23)  评论(0)    收藏  举报