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

如何在RecyclerView的列表项中显示其创建时间?

没问题,要实现你图里的列表项显示创建时间的效果,咱们一步步来修改代码就行:

1. 确保数据模型包含创建时间字段

首先你的Incidencia类需要添加一个存储创建时间的字段,建议用long类型存Unix时间戳,这样后续格式化很方便。记得保留空构造函数(Firebase序列化必须):

public class Incidencia {
    public String departamento;
    public String prioridad;
    public String motivo;
    public long timestamp; // 新增时间戳字段

    // Firebase序列化需要的空构造函数
    public Incidencia() {}

    // 带参数的构造函数,创建时传入时间戳
    public Incidencia(String departamento, String prioridad, String motivo, long timestamp) {
        this.departamento = departamento;
        this.prioridad = prioridad;
        this.motivo = motivo;
        this.timestamp = timestamp;
    }
}
2. 更新ViewHolder添加时间控件

在你的IncidenciaViewHolder里新增一个用于显示时间的TextView,并完成控件绑定:

public class IncidenciaViewHolder extends RecyclerView.ViewHolder{
    TextView departamento;
    TextView prioridad;
    TextView motivo;
    TextView creationTime; // 新增时间显示控件

    public IncidenciaViewHolder(View itemView) {
        super(itemView);
        departamento = itemView.findViewById(R.id.departamento);
        prioridad = itemView.findViewById(R.id.prioridad);
        motivo = itemView.findViewById(R.id.motivo);
        creationTime = itemView.findViewById(R.id.creation_time); // 绑定时间控件
    }
}
3. 调整列表项布局(修复重叠问题+添加时间显示)

你原来的XML布局里多个LinearLayout都是match_parent,会导致所有TextView重叠在一起,咱们改成垂直布局,把时间放在列表项的右下角(符合你图里的效果):

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardUseCompatPadding="true"
    app:cardElevation="4dp">

    <!-- 改成垂直LinearLayout,合理排列元素 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@+id/departamento"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/prioridad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/motivo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <!-- 创建时间控件,放在底部靠右 -->
        <TextView
            android:id="@+id/creation_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="end"
            android:textColor="@android:color/darker_gray"
            android:textSize="14sp" />

    </LinearLayout>
</androidx.cardview.widget.CardView>
4. 在绑定数据时格式化并显示时间

回到VerIncidencia的onBindViewHolder方法里,把时间戳转换成用户可读的日期格式,然后设置到对应的TextView上:

@Override
protected void onBindViewHolder(@NonNull IncidenciaViewHolder holder, final int position, @NonNull final Incidencia empresa) {
    holder.departamento.setText(empresa.departamento);
    holder.prioridad.setText(empresa.prioridad);
    holder.motivo.setText(empresa.motivo);

    // 将时间戳格式化为友好的日期时间字符串
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    String formattedTime = sdf.format(new Date(empresa.timestamp));
    holder.creationTime.setText(formattedTime);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(VerIncidencia.this, VerIncidenciaCompleta.class);
            intent.putExtra("INCIDENCIA_KEY", getRef(position).getKey());
            startActivity(intent);
        }
    });
}
额外提醒:存入数据时要记录时间戳

最后别忘了,当你往Firebase数据库添加新的Incidencia时,要把当前时间戳也存进去,比如:

// 示例:添加新的Incidencia到数据库
String uid = FirebaseAuth.getInstance().getUid();
DatabaseReference incidenciaRef = FirebaseDatabase.getInstance().getReference().child("incidencia").child(uid);
String key = incidenciaRef.push().getKey();
long timestamp = System.currentTimeMillis(); // 用客户端当前时间
// 或者用Firebase服务器时间:Map<String, Object> timestamp = ServerValue.TIMESTAMP;
Incidencia newIncidencia = new Incidencia("你的部门", "优先级", "问题原因", timestamp);
incidenciaRef.child(key).setValue(newIncidencia);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:52:40