我想知道是否有可能从Firestore上的一个包含TimeStamp字段的集合中返回文档。我能够返回所有其他String字段,但不能返回TimeStamp。以下是我为结合使用每种类尝试并实现此回收器视图的代码,这些视图包含Firestore上的Session集合中的TimeStamp值:
MyRecyclerViewAdapter类:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewHolder> {
DeleteSession deleteSession;
ArrayList<Session> sessionArrayList;
public MyRecyclerViewAdapter(DeleteSession deleteSession, ArrayList<Session> sessionArrayList) {
this.deleteSession = deleteSession;
this.sessionArrayList = sessionArrayList;
}
@NonNull
@Override
public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(deleteSession.getBaseContext());
View view = layoutInflater.inflate(R.layout.single_row, parent, false);
return new MyRecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerViewHolder holder, final int position) {
holder.mModule.setText(sessionArrayList.get(position).getModule());
holder.mTitle.setText(sessionArrayList.get(position).getTitle());
holder.mDate.setText(sessionArrayList.get(position).getDate());
holder.mRoom.setText(sessionArrayList.get(position).getRoom());
//holder.mStartTime.setText(sessionArrayList.get(position).getStartTime()); THIS IS A TIMESTAMP IN FIRESTORE
//
holder.mDeleteSession.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteSession(position);
}
});
}
private void deleteSession(int position) {
}
@Override
public int getItemCount() {
return sessionArrayList.size();
}
}
我的RecyclyerViewHolder类:
public class MyRecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView mModule, mTitle, mDate, mRoom, mStartTime;
public Button mDeleteSession;
public MyRecyclerViewHolder(View itemView) {
super(itemView);
mModule = itemView.findViewById(R.id.MODULE);
mTitle = itemView.findViewById(R.id.TITLE);
mDate = itemView.findViewById(R.id.DATE);
mRoom = itemView.findViewById(R.id.ROOM);
mStartTime = itemView.findViewById(R.id.START);
mDeleteSession = itemView.findViewById(R.id.DELETEBUTTON);
}
}
我的DeleteSession类:
public class DeleteSession extends AppCompatActivity {
FirebaseFirestore db;
RecyclerView mRecyclerView;
ArrayList<Session> sessionArrayList;
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_session);
sessionArrayList = new ArrayList<>();
setUpRecyclerView();
setUpFireBase();
loadDataFromFirebase();
}
private void setUpFireBase() {
db = FirebaseFirestore.getInstance().getInstance();
}
private void setUpRecyclerView(){
mRecyclerView = findViewById(R.id.mRecyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void loadDataFromFirebase(){
if (sessionArrayList.size()>0)
sessionArrayList.clear();
db.collection("Session").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot documentSnapshot: task.getResult()){
Session session = new Session(documentSnapshot.getString("module"),
documentSnapshot.getString("title"), documentSnapshot.getString("date"),
documentSnapshot.getString("room"), documentSnapshot.getDate("startTime"),
documentSnapshot.getDate("endTime"));
sessionArrayList.add(session);
}
adapter = new MyRecyclerViewAdapter(DeleteSession.this, sessionArrayList);
mRecyclerView.setAdapter(adapter);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(DeleteSession.this, "Problem occured",Toast.LENGTH_SHORT).show();
}
});
}