1

I've finally started to understand a lot of info regarding FireStore, but I'm wondering if I can get some assistance.

If I had a setup similar to or like this:

          races

                Android

                      name: Android

                      size: medium

                       stats          <---- this is the map

                                str: 10

                                sex: 12.... (more values)

How would I parse this? I am looking to make specific TextViews apply values found in the database so that I can simply update the database and my app will populate those values so that hard coding and code updating won't be nearly as troublesome in the future.

I currently use something like this:

val androidRef = db.collection("races").document("Android") 
androidRef.get().addOnSuccessListener { document ->
if (document != null) {
     oneOfTheTextViews.text = document.getString("str") 
} else { 
}

The issue is currently I can only seem to access from collection (races) / document (android) / then a single field (I have "str" set as a single field, not part of a map or array)

What would the best practice be to do this? Should I not nest them at all? And if I can reference said nesting/mapping/array, what functions need to be called? (To be clear, I am not asking only whether or not it is possible - the reference guides and documents allude to such - but what property/class/method/etc needs to be called in order to access only one of those values or point to one of those values?).

Second question: Is there a way to get a list of document names? If I have several races, and simply want to make a spinner or recycler view based on document names as part of a collection, can I read that to the app?

4
  • It is better to create new node. Separate the stats from the node races Commented Nov 6, 2019 at 3:03
  • So races is the collection, and Android is the document. You're suggesting make stats its own collection, or document? Each document will only possess perhaps 10~20 fields, if that (that's actually on the larger end). Commented Nov 6, 2019 at 5:52
  • I suggest to make stats out from the races nodes. Commented Nov 6, 2019 at 7:56
  • Is there an answer to my second question? Making documents in a collection into an array to use for a spinner? Commented Nov 6, 2019 at 8:14

1 Answer 1

4

What would the best practice be to do this?

If you want to get the value of your str property which is nested within your stats map, please change the following line of code:

oneOfTheTextViews.text = document.getString("str")

to

oneOfTheTextViews.text = document.getString("stats.str")

If your str property is a number and not a String, then instead of the above line of code please use this one:

oneOfTheTextViews.text = document.getLong("stats.str")

Should I not nest them at all?

No, you can nest as many properties as you want within a Map.

Is there a way to get a list of document names?

Yes, simply iterate the collection and get the document ids using getId() function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.