You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为基于Firebase Firestore和RecyclerView的Android笔记应用添加笔记保存日期(dd/MM/yy格式)并在RecyclerView中展示

解决方案:给Firebase笔记添加日期存储与RecyclerView显示

没问题,我来帮你搞定这个日期添加和显示的功能,分两步走:先把日期存进Firestore,再在RecyclerView里展示出来。

1. 修改添加笔记代码,存储日期

首先在你的AddWellbeingActivity里,当创建笔记数据时,加上当前日期的字段。我们用SimpleDateFormat生成你需要的dd/MM/yy格式:

先添加必要的导入(如果还没加的话):

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

然后在保存笔记的点击逻辑里,生成日期并加入到Firestore的存储Map中:

savenote.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String title = createtitleofnote.getText().toString();
        String content = createcontentofnote.getText().toString();
        if(title.isEmpty() || content.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Both fields are Required", Toast.LENGTH_SHORT).show();
        } else {
            // 生成当前日期,格式为dd/MM/yy
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy", Locale.getDefault());
            String currentDate = sdf.format(new Date());

            DocumentReference documentReference = firebaseFirestore.collection("notes").document(firebaseUser.getUid()).collection("myNotes").document();
            Map<String, Object> note = new HashMap<>();
            note.put("title",title);
            note.put("content", content);
            note.put("date", currentDate); // 新增日期字段

            documentReference.set(note).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(getApplicationContext(), "Wellbeing entry saved", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(AddWellbeingActivity.this, WellbeingActivity.class));
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getApplicationContext(), "Wellbeing entry failed to save", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
});

这样每次保存笔记时,当前日期就会和标题、内容一起存进Firestore的对应文档里了。

2. 在RecyclerView中显示日期

接下来要让日期在RecyclerView的每个笔记项里显示出来,需要完成这几件事:

2.1 优化笔记数据模型(推荐用实体类)

如果你还没有专门的笔记实体类,建议创建一个,方便后续数据处理:

public class Note {
    private String title;
    private String content;
    private String date;

    // 空构造函数(Firestore序列化必须)
    public Note() {}

    // 带参数的构造函数
    public Note(String title, String content, String date) {
        this.title = title;
        this.content = content;
        this.date = date;
    }

    // Getter和Setter方法
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public String getContent() { return content; }
    public void setContent(String content) { this.content = content; }
    public String getDate() { return date; }
    public void setDate(String date) { this.date = date; }
}

2.2 更新RecyclerView的item布局

打开你的笔记项布局文件(比如item_note.xml),添加一个TextView用来显示日期,比如放在标题下方:

<!-- 原有的标题、内容TextView之后 -->
<TextView
    android:id="@+id/noteDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:textColor="@color/gray"
    android:layout_marginTop="4dp"/>

2.3 更新RecyclerView适配器

修改你的适配器类(比如NoteAdapter),在ViewHolder里绑定日期字段:

首先在ViewHolder中初始化日期TextView:

public class NoteViewHolder extends RecyclerView.ViewHolder {
    TextView noteTitle, noteContent, noteDate;

    public NoteViewHolder(@NonNull View itemView) {
        super(itemView);
        noteTitle = itemView.findViewById(R.id.noteTitle);
        noteContent = itemView.findViewById(R.id.noteContent);
        noteDate = itemView.findViewById(R.id.noteDate); // 初始化日期TextView
    }
}

然后在onBindViewHolder方法中设置日期内容:

@Override
public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
    Note currentNote = noteList.get(position);
    holder.noteTitle.setText(currentNote.getTitle());
    holder.noteContent.setText(currentNote.getContent());
    holder.noteDate.setText(currentNote.getDate()); // 显示日期
}

如果之前你是用HashMap直接处理Firestore数据,那在获取笔记列表时记得取出date字段:

// 示例:在WellbeingActivity中获取笔记的逻辑
firebaseFirestore.collection("notes").document(firebaseUser.getUid()).collection("myNotes")
        .get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                List<Note> noteList = new ArrayList<>();
                for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
                    String title = document.getString("title");
                    String content = document.getString("content");
                    String date = document.getString("date"); // 获取日期字段
                    noteList.add(new Note(title, content, date));
                }
                // 初始化适配器并绑定到RecyclerView
                NoteAdapter adapter = new NoteAdapter(noteList);
                recyclerView.setAdapter(adapter);
            }
        });

这样处理后,你的笔记在保存时会自动带上当前日期,并且在RecyclerView的每个笔记项里都会显示出来啦。

内容的提问来源于stack exchange,提问作者ComputingStudentWhoCantCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:52:26