按时间戳升序对 Firestore 数据进行排序

8 浏览
0 Comments

按时间戳升序对 Firestore 数据进行排序

我将应用程序中发布的想法存储在Firestore中。数据以这样的方式存储在Firestore中:Ideas/{documentID}/IdeaObject。问题是当我检索数据时,它们并没有按照发布时间进行排序。检索到的想法是根据它们的documentID的id进行排序的,这个id是Firestore自动生成的。我在我的模型类中使用了ServerTimestamp,并且在检索数据时,我使用Firestore引用的orderBy方法,但仍然没有排序。

Idea.java

public class Idea {
    @ServerTimestamp
    private Date date;
    private String title, idea, timeCommitment, ideaStage, postedBy, website, videoPitch;
    private int views, favorites;
    private ArrayList lookingFor = new ArrayList<>();
    private ArrayList tags = new ArrayList<>();
    private String userID;
    private String timeStamp;
    public Idea() {
    }
    public Idea(String title, String idea, String timeCommitment, String ideaStage, String postedBy, String website, String videoPitch, int views, int favorites, ArrayList lookingFor, ArrayList tags, String userID, String timeStamp) {
        this.title = title;
        this.idea = idea;
        this.timeCommitment = timeCommitment;
        this.ideaStage = ideaStage;
        this.postedBy = postedBy;
        this.website = website;
        this.videoPitch = videoPitch;
        this.views = views;
        this.favorites = favorites;
        this.lookingFor = lookingFor;
        this.tags = tags;
        this.userID = userID;
        this.timeStamp = timeStamp;
    }

发布想法的方法

  private void postIdea() {
        final String ideaID = UUID.randomUUID().toString();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Idea ideas = new Idea(title, idea, timeCommitment, ideaStage, AppValues.fullName, website, videoPitch, 0, 0, lookingFor, tags, AppValues.userId, "" + timestamp.getTime());
        firestoreDb.collection("ideas")
                .document(ideaID)
                .set(ideas)
                .addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        postIdeaUser(ideaID);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        hideLoadingDialog();
                        showToast(e.getMessage());
                    }
                });
    }

按时间检索所有想法

   firestoreDb.collection("ideas")
                .orderBy("timeStamp", Query.Direction.ASCENDING)
                .get()
                .addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful()) {
                            ideaArrayList = new ArrayList<>();
                            ideaArrayList.clear();
                            for (DocumentSnapshot documentSnapshot : task.getResult()) {
                                Idea idea = documentSnapshot.toObject(Idea.class);
                                if (!idea.getUserID().equals(AppValues.userId)) {
                                    ideaArrayList.add(idea);
                                }
                            }
                            callAdapter();
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                            progressBar.setVisibility(View.GONE);
                            swipeRefresh.setEnabled(true);
                            errorText.setVisibility(View.VISIBLE);
                        }
                    }
                }); 

我想实现的是检索所有想法,并按照时间戳值升序排列。

0