简体   繁体   English

从 Firestore 检索嵌套对象并放入 Android 中的 RecycleView?

[英]Retrieve a nested object from Firestore and put into RecycleView in Android?

I am trying to populate a RecylerView in Android with attached image data structure in a single document.我正在尝试在单个文档中使用附加的图像数据结构填充 Android 中的RecylerView I want to list out each name, district, image for each ID.我想列出每个 ID 的每个名称、地区、图像。

It needs to be done without breaking down in the sub-collection.它需要在不分解子集合的情况下完成。 Is there any possible smart way to populate the RecylerView .是否有任何可能的智能方法来填充RecylerView Thanks谢谢

Here is the sample data I am using.这是我正在使用的示例数据。 [![enter image description here][1]][1] [![在此处输入图像描述][1]][1]

Edit 1: I tried to implement the following, does it work?编辑 1:我尝试执行以下操作,它有效吗?

private void setUpRecyclerView(View view) {

        CheckinList = new ArrayList<>();
        notebookRef.get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>(){

            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                    user user = documentSnapshot.toObject(user.class);

                    HashMap<String, HashMap> about = user.getAbout();
                    for(Map.Entry<String, HashMap> entry : about.entrySet()) {
                        String key = entry.getKey();
                        HashMap<String, HashMap> value = entry.getValue();
                        String Name = "";
                        String District = "";
                        String imageUrl = "";
                        for (Map.Entry<String, HashMap> entry3 : value.entrySet()) {
                            String key3 = entry3.getKey();
                            if (key3 == "name"){
                                Name = entry3.getValue().toString();
                            } else if (key3 == "district"){
                                District = entry3.getValue().toString();
                            } else if (key3 == "imageurl") {
                                imageUrl = entry3.getValue().toString();
                            }
                        }
                        CheckinList.add(new Checkin(Name, District, imageUrl));

                        }

                    }

                }

        });

        adapter = new NoteAdapter(CheckinList, mContext);

        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
    }```

full data scheme screenshot
[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/2Jspj.jpg
  [2]: https://i.stack.imgur.com/iwsat.jpg

You need to use nested for loop if number of ID is not large.如果 ID 数量不多,则需要使用嵌套 for 循环

if you have access of database you should change the schema to something like this如果您有权访问数据库,您应该将架构更改为这样的

Document(randomID) -> Collection (about) -> Document (IDs) = Data (name/district/image);

after this you can simply make a nested Firebase query to access data.在此之后,您可以简单地进行嵌套的 Firebase 查询来访问数据。

Please provide more information if you need help with Firebase query with my given schema or if you want to use your own schema and get the data.如果您需要使用我的给定架构进行 Firebase 查询方面的帮助,或者您想使用自己的架构并获取数据,请提供更多信息。

Edit 1编辑 1

not suggested as this is not the best way of getting data不建议,因为这不是获取数据的最佳方式

FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();

firebaseFirestore
    .collection("your collection")
    .document("your doc).get()
    .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()){
            int docSize = 10; //size of IDs (assuming 10)
            for (int index = 0; index <= docSize; index++){
                HashMap<String, Object> dataMap = (HashMap<String, Object>) task.getResult().get("1000000"+index);
                //you have data in dataMap
            }
        }
    }
});

Sorry not sure about the result as I don't have full information, you need to modify the code.抱歉不确定结果,因为我没有完整的信息,您需要修改代码。 Please edit my answer if you get success.如果您成功,请编辑我的答案。

Still suggesting to change the schema仍然建议更改架构

Hope this will help!希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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