1

I successfully managed to get the strings that are stored in the array of the database (Log.d("TAG", entry.getValue().toString());shows the values of the strings of the array).

The strings are auto-generated document ids stored in an Array in Firestore, so the goal is getting 3 ids so I can make 3 queries to get the actual documents.

I am now stuck on how to attach the strings I got from Firestore to an ArrayList and then get 3 individual random strings from it.

I am relatively new to ArrayLists so any help is much appreciated!

Here is my code:

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

            DocumentSnapshot document = task.getResult();

            Map<String, Object> map = document.getData();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (entry.getKey().equals("questions")) {
                    Log.d("TAG", entry.getValue().toString());

// Create function that gets 3 different values from the ArrayList and store them in 3 different Strings

                }
            }
        }
    }
});

Here is how I structured the database. Currently I am getting the ArrayList so I don't have so much reads. On the right you can see the documents I want to query when I have selected 3 random document ids from the ArrayList:

enter image description here

Kaiser
  • 606
  • 8
  • 22
  • 1
    Please show us a screenshot of the document that contains the `questions` array, including the root collection. – Alex Mamo Jan 09 '20 at 11:44
  • Hello @AlexMamo, I shared a screenshot – Kaiser Jan 09 '20 at 11:48
  • How is `questionRef` defined? What's the value of `tvCat1` in that reference? – Alex Mamo Jan 09 '20 at 11:50
  • You got me there, I had a typo in my reference ;) The second approach is now working but how can I use this to get 3 random ids from it? – Kaiser Jan 09 '20 at 11:59
  • 1
    Good to hear that it worked.Check [this](https://stackoverflow.com/questions/5034370/retrieving-a-random-item-from-arraylist) out. – Alex Mamo Jan 09 '20 at 12:05
  • If you are choosing three, you will want to take the first item out of the list before choosing the second. – Jeremy Kahan Jan 09 '20 at 13:11
  • @JeremyKahan I updated my question with the code I am currently using – Kaiser Jan 09 '20 at 13:38
  • okay. That looks like a reasonable approach (if you happen to choose the same one again, not using it). – Jeremy Kahan Jan 09 '20 at 23:23
  • Couple of questions: 1. Why do you need to iterate over entries of map, can you use map.contains() and map.get() instead, this would simplify your code 2. Once you get first question, you can remove it from the list by calling remove(index) and generate random again, then you need to check if you have already taken that string (assuming there are no duplicates) 3. You need not create new Random() everytime, you can create once and reuse it. Seems code can be simplified further, let me know if you need help – Ankit Dixit Jan 20 '20 at 06:45
  • @AnkitDixit Thank you for your comment. Unfortunately I do not have any clue how to implement this feature, do you have any code example on how to implement your approach? – Kaiser Jan 20 '20 at 18:15

1 Answers1

1

I found a way to achieve the preferred result. I posted an answer to this existing question to prevent posting identical answers.

Kaiser
  • 606
  • 8
  • 22