Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

java - Order Firestore data by TimeStamp in Ascending order

I am storing Ideas posted by the application in Firestore. The data is stored in Firestore like this Ideas/{documentID}/IdeaObject. The issue is when I retrieve the data it is not sorted w.r.t time it was posted. The ideas that are retrieved are in according to the id's of their documentID which is automatically create by Firestore. I have used ServerTimestamp in my Model Class and also when I retrieve it, I use the orderBy method with my Firestore reference but still nothing.

Idea.java

public class Idea {
    @ServerTimestamp
    private Date date;
    private String title, idea, timeCommitment, ideaStage, postedBy, website, videoPitch;
    private int views, favorites;
    private ArrayList<String> lookingFor = new ArrayList<>();
    private ArrayList<String> 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<String> lookingFor, ArrayList<String> 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;
    }

Ideas Posting Method

  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<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        postIdeaUser(ideaID);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        hideLoadingDialog();
                        showToast(e.getMessage());
                    }
                });
    }

Retrieving all Ideas by Time

   firestoreDb.collection("ideas")
                .orderBy("timeStamp", Query.Direction.ASCENDING)
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> 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);
                        }
                    }
                }); 

What I want to achieve is retrieve all the ideas and have them ordered ascending by the TimeStamp value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...