gpt4 book ai didi

android - 如何在 RecyclerView 中显示 Firestore 时间戳(日期和时间)

转载 作者:行者123 更新时间:2023-11-29 18:31:07 25 4
gpt4 key购买 nike

我正在构建一个银行应用程序并希望显示帐户交易的历史记录,当我将时间保存到 Firestore 时,它​​的格式是时间戳,但是当我尝试在我的 RecyclerView 中显示它时,它只有几秒和纳秒。

如何显示日期和时间?

我的 recyclerView 方法:

    private void setUpRecyclerView() {
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

CollectionReference accountTransRef = db.collection(userId).document("accounts")
.collection("accounts").document(accountID).collection("transactions");

Query query = accountTransRef.orderBy("tTimestamp",Query.Direction.DESCENDING);
FirestoreRecyclerOptions<AccountTransactionModel> options = new FirestoreRecyclerOptions.Builder<AccountTransactionModel>()
.setQuery(query, AccountTransactionModel.class)
.build();

adapter = new AccountTransferAdapter(options);

RecyclerView recyclerView = findViewById(R.id.rwTransactionList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}

我的交易模型

public class AccountTransactionModel {
private String tType,tAccountToId, tDocumentId;
private Timestamp tTimestamp;
private double tAmount;

public AccountTransactionModel() {
}

public AccountTransactionModel(String tType, String tAccountToId, String tDocumentId, Timestamp tTimestamp, double tAmount) {
this.tType = tType;
this.tAccountToId = tAccountToId;
this.tDocumentId = tDocumentId;
this.tTimestamp = tTimestamp;
this.tAmount = tAmount;
}

public String gettType() {
return tType;
}

public String gettAccountToId() {
return tAccountToId;
}

@Exclude
public String gettDocumentId() {
return tDocumentId;
}

public void settDocumentId(String tDocumentId) {
this.tDocumentId = tDocumentId;
}

public Timestamp gettTimestamp() {
return tTimestamp;
}

public double gettAmount() {
return tAmount;
}
}

我的适配器

public class AccountTransferAdapter extends FirestoreRecyclerAdapter<AccountTransactionModel, AccountTransferAdapter.TransferHolder > {


public AccountTransferAdapter(@NonNull FirestoreRecyclerOptions<AccountTransactionModel> options) {
super(options);
}

@Override
protected void onBindViewHolder(@NonNull TransferHolder holder, int position, @NonNull AccountTransactionModel model) {
holder.tvTransListAmount.setText(Double.toString(model.gettAmount()));
holder.tvTransListType.setText(model.gettType());
holder.tvTransListTime.setText(model.gettTimestamp().toString());
}

@NonNull
@Override
public TransferHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.transactions_list,viewGroup,false);
return new TransferHolder(v);

}

class TransferHolder extends RecyclerView.ViewHolder{
TextView tvTransListAmount;
TextView tvTransListTime;
TextView tvTransListType;

public TransferHolder(@NonNull View itemView) {
super(itemView);
tvTransListAmount = itemView.findViewById(R.id.trans_list_amount);
tvTransListTime = itemView.findViewById(R.id.trans_list_time);
tvTransListType = itemView.findViewById(R.id.trans_list_type);
//tvAccName = itemView.findViewById(R.id.tvAccountName);
//tvAccBalance = itemView.findViewById(R.id.tvAccountBalance);
}
}
}

我的 View 、应用程序和 Firestore 中显示的内容:

Timestamp(seconds=1558437203,nanoseconds=72000000)

what it looks like in the app What my Firestore data looks like

最佳答案

如果 Timestamp 是 firebase 包,那么你可以使用 Timestamp#toDate() 函数

model.gettTimestamp().toDate().toString() 应该给你整个日期

关于android - 如何在 RecyclerView 中显示 Firestore 时间戳(日期和时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com