Android中如何在RecyclerView卡片中显示自动日期时间并避免重复(Firebase Database场景)
Hey there! Let's get your date/time issue sorted out. The core problem right now is that you're generating the current date and time every time a RecyclerView item is bound (when onBindViewHolder runs), instead of storing a unique timestamp for each item in Firebase. That's why all cards show the same time—they're all using the time when the view was created, not the time the item was added to your database.
Here's how to fix this step by step:
1. Update your member class to store both date and time
Right now, your member only has a time field. Let's add a date field so we can save both values for each item:
public class member { String description, title, imageuri, time, date; // Keep the empty constructor for Firebase deserialization public member() {} // Updated constructor to include date and time public member(String description, String title, String imageuri, String time, String date) { this.description = description; this.title = title; this.imageuri = imageuri; this.time = time; this.date = date; } // Add getter/setter for date public String getDate() { return date; } public void setDate(String date) { this.date = date; } // Keep all your existing getters/setters public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImageuri() { return imageuri; } public void setImageuri(String imageuri) { this.imageuri = imageuri; } }
2. Save date/time to Firebase when adding new items
When you create a new member to upload to Firebase, generate the date and time once (at the moment of creation) and pass them into the member object. Here's an example of how your upload code should look:
// Generate date/time when creating the new item String currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()).format(new Date()); String currentTime = new SimpleDateFormat("hh:mm:a", Locale.getDefault()).format(new Date()); // Create your member with the generated date/time member newMember = new member(yourDescription, yourTitle, yourImageUri, currentTime, currentDate); // Upload to Firebase Database DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference("your_collection_name"); databaseRef.push().setValue(newMember);
Replace yourDescription, yourTitle, yourImageUri, and your_collection_name with your actual values.
3. Modify your Adapter to use the stored date/time
Instead of generating date/time in onBindViewHolder, pull the saved values directly from the member model:
@Override protected void onBindViewHolder(@NonNull Adapter.myViewHolder holder, int position, @NonNull member model) { holder.mheadView.setText(model.getTitle()); holder.mdesView.setText(model.getDescription()); Glide.with(holder.imageView.getContext()).load(model.getImageuri()).into(holder.imageView); holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AppCompatActivity activity = (AppCompatActivity) v.getContext(); activity.getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container_view, new DetailsFragment(model.getTitle(), model.getImageuri())) .addToBackStack(null) .commit(); } }); // Use the stored date/time from Firebase instead of generating new ones holder.Date.setText(model.getDate()); holder.Time.setText(model.getTime()); }
Why this works:
- Each item now has its own unique date/time saved in Firebase, tied to when it was created.
- When RecyclerView binds items, it pulls the pre-saved values instead of generating new ones every time, so no more duplication.
Quick notes:
- If you already have existing items in your Firebase database, you'll need to update them with date/time values (either manually in the Firebase console or via a one-time batch update in your app).
- Make sure your Firebase security rules allow read/write access (adjust them accordingly for production!).
内容的提问来源于stack exchange,提问作者ajay more

