简体   繁体   中英

Java - How to specify the order that Firestore retrieves field names?

I'm trying to retrieve all field names from a document (the document name is the userID) under a collection called "taskboards". I want Firestore to retrieve these field names in a specific order but I don't know how.

Here is the following code:

userID = fAuth.getCurrentUser().getUid();
DocumentReference userTaskboardRef = fStore.collection("taskboards").document(userID)

userTaskboardRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if(task.isSuccessful()){

            List<String> columnsList = new ArrayList<>();

            Map<String, Object> map = task.getResult().getData();
            for(Map.Entry<String, Object> entry : map.entrySet()){
                columnsList.add(entry.getKey());
                Log.d("TAG", entry.getKey());
                Log.d("TAG", entry.getValue().toString());

            }

For example:

I would like Firestore to retrieve the following field names in the order "To Do, Doing, Done". Here is a picture of how the field names are stored in my Firestore.

在此处输入图片说明

The above code works and retrieves the field names but it retrieves them in the order "Doing, To Do, Done". Here is a picture of TAG showing the order.

在此处输入图片说明

I'm quite new to Android Studio so can anybody help me out? Is it even possible to specify the order in which the field names are retrieved?

The order of fields in a document is undefined, as is the order of entries in a Map .

The screenshot of the Firebase console in your questions shows the fields in alphabetical order. If you want to replicate that in your code, you can sort the keys with a TreeMap :

Map<String, Object> map = new TreeMap<>(task.getResult().getData());
for(Map.Entry<String, Object> entry : map.entrySet()){
    ...

Also see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM