How to read nested arrays and maps in Firestore? [closed] - android

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to read nested array and maps in android studio (Java) from firestore but could not able to iterate through it. I am new to it so can anyone please help me. Thank you :)
Edited: How can I iterate through "options" as it is an array in map.
I've achieved the rest by saving them in HashMap and iterating through it.

First, make a data class for your JSON. You can do it with this plugin
Let's say you have something like that (this example done in kotlin):
data class Data(
val id: Int,
val lists: List<Company>,
val name: String
)
data class Company(
val q: String,
val type: String,
val optType: Int?,
val data: List<Options>?
)
data class Options(
val option : String
)
After that, you can do a loop following
for (Company list : data.getLists()) { // data is your object. GetList returns first array
for (Options datum : list.getData()) { // from list you will get next array.
System.out.println(datum.getOption()); // here you will get item from nested array
}
}

Related

Arraylist value changes automatically KOTLIN

I have an arraylist inside another arraylist , when i enter values, value of the previous arraylist automatically changes to last entered value.
Code is as follows
Question.class
data class Question(
var id: String,
var question: String,
var correct: String,
var answer: ArrayList<String>
)
MainActivity
answers.add("1234")
answers.add("234")
questions.add(Question("1","1","1",answers))
Log.d("12345",questions.toString())
answers.clear()
answers.add("5678")
answers.add("789")
questions.add(Question("2","2","2",answers))
Log.d("12345",questions.toString())
o/p
D/12345: [Question(id=1, question=1, correct=1, answer=[1234, 234])]
D/12345: [Question(id=1, question=1, correct=1, answer=[5678, 789]), Question(id=2, question=2, correct=2, answer=[5678, 789])]
You only have one list of answers here. Each question gets a reference to that single list. Instead, you should create a new list of answers for each question. To understand this better, I suggest you read about references in Kotlin.

How do I handle this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
Error on my flutter code regarding a dropdown
Exception has occurred.
_CastError (type 'List' is not a subtype of type 'String?' in type cast)
You are trying to cast list to a string thats why you get _CastError, when you use "[]" in dart, you actually declare a list, so either you need to change type String? selectedPlan to List<String>? selectedPlan or just remove the brackets like String? selectedPlan = 'FIXED DEPOSIT SAVINGS';
And please share code as code next time :)

Create a Map with keys from 1 to 100 odd numbers and Values ​from even numbers from 1 to 100 in Kotlin [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Thanks in advance! It's the first time I am asking in StackOverflow
Keys and values in ascending order:
val result = (1..100 step 2).associateWith { it + 1 }
println(result) // prints {1=2, 3=4, 5=6, 7=8, ..., 99=100}
If you need only the keys in ascending order and the values shuffled:
val result = (1..99 step 2)
.zip((2..100 step 2).shuffled())
.toMap()
println(result) // {1=74, 3=18, 5=40, 7=14, 9=76, 11=34, ..., 99=38}
If you need both the keys and the values shuffled:
val result = (1..99 step 2).shuffled()
.zip((2..100 step 2).shuffled())
.toMap()
println(result) // {23=34, 39=70, 61=24, 77=36, 83=86, ..., 49=74}

Android studio Firebase how to show other users information? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to show a profile page of other users with their data. I have data stored in the Realtime Database. I can load my own data but have do I do that for specific users and show it?
For example the current user I use the following code to get the datasnapshot
firebaseDatabase.getReference("users").child(firebaseAuth.currentUser!!.uid)
Not sure how you database is structured but you should be ale to do something like this:
val userRef = database.getReference("users")
userRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val data = snapshot.value as? Map<String, *>?
// data is a map of { uid to User }
}
override fun onCancelled(error: DatabaseError) {
Log.e(TAG, "Loading user data failed: \n" + error.message)
}
})
Basically do it the same way you get the individual user, but pull from the parent node to get all of them in the form of a map

How to split a string into 2 different variables based on a delimiter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I split one variable into two using the split function.Now I want to store the split variable as 2 different variables.
For example, I split the string 'input' into 2 parts. Now I split the variable with the delimiter being a forward slash (/). I want to store the text which was before the slash as 'input_1' and the text after the slash as 'input_2'. How do I do this?
Please provide some code too, I'll be grateful.
When you split your String, it will return an Array. All you need to do is grab each element from the Array.
Note: this can be dangerous if you don't know what input you're taking in, consider checking the actual length of the result.
val input = "before/after"
// Split will return an array
val split = input.split("/")
val before = split[0] // First element
val after = split[1] // Second element
I guess you're doing this:
var split = input.split('/')
var pre_input = split[0]
var post_input = split[1]
For the case, your input has only one slash.

Categories

Resources