1

I am trying to populate a RecylerView in Android with attached image data structure in a single document. I want to list out each name, district, image for each ID.

It needs to be done without breaking down in the sub-collection. Is there any possible smart way to populate the RecylerView. Thanks

Here is the sample data I am using. [![enter image description here][1]][1]

Edit 1: I tried to implement the following, does it work?

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
Howard Lau
  • 163
  • 1
  • 11

1 Answers1

0

You need to use nested for loop if number of ID is not large.

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.

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.

Edit 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!

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
  • thanks for reply. for the nested for loop, is that i need to make a for loop on the hashmap and then make another loop for the inner hashmap? Afterward, i then add/convert the result hashmap into an array list. Lastly, i put the array list into the recyclerview adapter to populate it. is my understanding correct? – Howard Lau Jan 09 '20 at 05:35
  • after trying to implement your schema in Firebase it is not feasible, can you change your Firestore structure? It is not following proper JSON formatting, you should use my suggested or something similar as your schema/structure – Rahul Gaur Jan 09 '20 at 06:04
  • Thanks. I just wonder, i think i can do that in realtime database. realtime datebase can be nested for several layers. just like a chat room app. i thought it should be similar in firestore. please correct me if I misunderstood this part. – Howard Lau Jan 09 '20 at 06:15
  • in realtime database you do not have to create another collection by yourself, but here you have to do that part. `Collection(mainCollection) ->Document(randomID) -> Collection (about) -> Document (IDs) = Data (name/district/image);` this is what you have to do in firebase to get the same result. It might look like some extra steps but this is for the better – Rahul Gaur Jan 09 '20 at 06:27
  • i gave a trial by making couple loop. will it work? reference: https://stackoverflow.com/questions/4234985/how-to-for-each-the-hashmap – Howard Lau Jan 09 '20 at 08:41
  • I don't know if it will work or not, you can test and try it, if it will work post the solution as an answer – Rahul Gaur Jan 09 '20 at 09:01
  • Hey @RahulGaur, i got the solution. Just have one follow up question, see if you can help. Thanks again. – Howard Lau Jan 10 '20 at 02:54