0

So, I have created data with a list of documents on Firestore, and I want to retrieve a single document randomly, whenever the Android activity is invoked.

Like for example, I have the following set of documents in the collection "people"

Alex

Abhishek

Nandini

Nancy

and in my Android activity, I have a TextView which will show each of the collection name randomly, whenever I invoke that activity, like when I open the activity, it will show "Nancy" on the textview and again when I reopen, it would show "Abhishek!"

Hope you understood my question, and it will be helpful and anyone can help me in that

Using JAVA code for coding my Android application!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Subhraneel
  • 15
  • 5

2 Answers2

0

I don't think firebase can get a random doc. You should use a field in your documents to handle that.

For example: if your application is simple, using an incremental number in each document should be enough as you could simply query a random number between 0 and the number of documents-1

C.C
  • 51
  • 2
  • 2
  • 7
  • like see all the documents are retrieved using collectionreference, and using for each, we get every document, I want to retrieve one single document in random on android studio! – Subhraneel Sep 05 '21 at 09:28
0

Retrive all the documents and store them in an arraylist and when user clicks the textview, pick a random document from the list. ArrayList.random() this returns a random element from the list.

Mihir Shah
  • 51
  • 3
  • I tried using random() in the arraylist but it shows, method random is not resolved like I created an arraylist questionlist and I wrote the code as follows - 'questionlist.random()' which is not working! – Subhraneel Sep 05 '21 at 10:44