I am building a chat app, and I am retrieving last chats with unread messages.
I am using firebase as my database and MVVM pattern.
So the problem is that I have to run 3 firebase requests in order to get HashMap with contact name and last unseen messages.
I am running the first function "getUsersChatList" that is located in the repository.
In that function I call the second function "getUsersLastChat(arrayList)" and I am passing the result of the last function.
From "getUsersLastChat(arrayList)" I call the last function "searchNumberOfMessages(user)" and I am passing the result of the previous function.
"searchNumberOfMessages" result is a List of HashMap with the contact name and the unread messages.
I declared at the top of my repository a MutableLiveData that stores the list of hashMap and I created a function to observe its changes,however,its not getting called because the value is not updated and I don't know why.
Here is what I have done:
Repository:
Top repository:
var newMessages = MutableLiveData<List<HashMap<String, Any?>>>()
getUsersChatList:
fun getUsersChatList(){
val arrayList = ArrayList<ChatList>()
chatListRef.child(currentUser!!.uid).addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (snapshot2 in snapshot.children) {
val singleListItem = snapshot2.getValue(ChatList::class.java)
if (singleListItem != null) {
arrayList.add(singleListItem)
}
}
getUsersLastChat(arrayList)
}
override fun onCancelled(error: DatabaseError) {
}
})
}
getUsersLastChat:
private fun getUsersLastChat(arrayList: ArrayList<ChatList>){
usersRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (snapshot2 in snapshot.children){
val user = snapshot2.getValue(User::class.java)
for (eachChatList in arrayList)
{
if (user!!.uid.equals(eachChatList.id))
{
searchNumberOfMessages(user)
}
}
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
This is where the problem,searchNumberOfMessages:
fun searchNumberOfMessages(user: User) : MutableLiveData<List<HashMap<String, Any?>>> {
val messageArray = ArrayList<String?>()
val hashMapArray = ArrayList<HashMap<String, Any?>>()
val hasMap = HashMap<String, Any?>()
chatRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (snapshot2 in snapshot.children) {
val chat = snapshot2.getValue(Message::class.java)
if (chat!!.receiver.equals(currentUser!!.uid) && chat.sender.equals(
user.uid)) {
if (chat.seen == false){
messageArray.add(chat.message)
}
}
}
hasMap["Sender"] = user.userName
hasMap["Messages"] = messageArray.size
hashMapArray.add(hasMap)
newMessages.value = hashMapArray
Log.e("newMessages", "newMessages: ${newMessages.value}") // Getting the current Values with No Problem
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
Log.e("newMessages2", "newMessages2: ${newMessages.value}") // Getting null on the same value and pass the null object to the viewModel
return newMessages
}
In the viewModel:
fun getUsersChatList(): MutableLiveData<List<HashMap<String, Any?>>> {
mainRepository.getUsersChatList()
return mainRepository.newMessages
}
And in the activity:
chatsListFragmentViewModel.getUsersChatList().observe(viewLifecycleOwner,object : Observer<List<HashMap<String, Any?>>?> {
override fun onChanged(t: List<HashMap<String, Any?>>?) {
Log.e("userMessagesHashMap","userMessagesHashMap ${t}")
}
})
This is the result in logcat:
2022-01-20 04:13:31.604 13145-13145/com.dapps.misronim E/newMessages2: newMessages2: null
2022-01-20 04:13:31.610 13145-13145/com.dapps.misronim E/newMessages2: newMessages2: null
2022-01-20 04:13:31.779 13145-16093/com.dapps.misronim D/FA: Application going to the background
2022-01-20 04:13:31.859 13145-13145/com.dapps.misronim E/newMessages: newMessages: [{Sender=Mario, Messages=4}]
2022-01-20 04:13:31.873 13145-13145/com.dapps.misronim E/newMessages: newMessages: [{Sender=Dj, Messages=2}]
2022-01-20 04:13:31.886 13145-13145/com.dapps.misronim E/newMessages: newMessages: [{Sender=Jfioo32856, Messages=0}]
Thank you very much for your time and help,I really appreciate it !
Log.e("newMessages2", "newMessages2: ${newMessages.value}")
return newMessages
}`
This code will return a null value because ondataChanged is not done yet. which means that you cannot return anything. Try observing newMessages instead
//in your viewModel
val messages = mainRepository.newMessages
fun getUsersChatList(){
mainRepository.getUserChatList()
}
//in your fragment
chatsListFragmentViewModel.getUserChatList()
chatsListFragmentViewModel.messages.observe(viewLifecycleOwner,object :
Observer<List<HashMap<String, Any?>>?> {
override fun onChanged(t: List<HashMap<String, Any?>>?) {
Log.e("userMessagesHashMap","userMessagesHashMap ${t}")
}
})
Related
I am looking for a way to populate my RecyclerView. I know how to populate a RecyclerView from a single node of my database but I wonder how can I get data from multiple nodes. In my app the user can select multiple hobbies. And I want to populate a recycler view depending which hobbies the user has selected.
Here is a picture of my user node from my database
As you can see my user has selected the hobbies Caffe's,Camping,Hiking
I have a node called Locations and there a six child nodes Caffe's,Camping,Hiking,Museums,Night life and restaurants. Here is my Locations node
I get users hobbies from this code block
val currentuid = FirebaseAuth.getInstance().currentUser!!.uid
val getCurrentUserHobbies =
FirebaseDatabase.getInstance().getReference("users/$currentuid/hobbies")
getCurrentUserHobbies.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val value = snapshot.getValue(String::class.java)
val valueAsString = value.toString()
if (valueAsString.contains("Hiking")){
}
}
override fun onCancelled(error: DatabaseError) {
}
})
If I want to get my recycler view to show all Hiking locations I do this:
val adapter = GroupAdapter<GroupieViewHolder>()
val hikingRef = FirebaseDatabase.getInstance().getReference("Locations/Hiking")
hikingRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (snap in snapshot.children) {
val recommendItems = snap.getValue(RecommendationsClass::class.java)
if (recommendItems != null) {
adapter.add(RecommendationsAdapter(recommendItems))
}
}
recommendationsRecyclerView.adapter = adapter
}
override fun onCancelled(error: DatabaseError) {
}
})
Now if the user has selected hiking and camping for example. I want my recycler view to show all locations from nodes Locations/Hiking and Locations/Camping. I tried multiple but nothing works. I am not going to write them here because it will take too much space. I am trying to keep my question as simple as possible.
Thanks in advance!
I unfortunately could not find any possible solution how to make it work. I had to do something different. What I did I copied sub nodes from my Locations node and put them in users/currentuid/Recommendations depending which hobbies my current user has.
Here is the code snipped that does that:
val currentuid = FirebaseAuth.getInstance().currentUser!!.uid
val getCurrentUserHobbies = FirebaseDatabase.getInstance().getReference("users/$currentuid/hobbies")
getCurrentUserHobbies.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val value = snapshot.getValue(String::class.java)
val valueAsString = value.toString()
if (valueAsString.contains("Caffe's")) {
val ref = FirebaseDatabase.getInstance().getReference("Locations/Caffe's/")
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (snap in dataSnapshot.children) {
val data = dataSnapshot.value
val newData = mapOf("Recommendations/${dataSnapshot.key}" to data)
FirebaseDatabase.getInstance().getReference("users")
.child(currentuid).updateChildren(newData)
}
}
override fun onCancelled(databaseError: DatabaseError) {
// Handle error
}
})
}
if (valueAsString.contains("Camping")) {
val ref = FirebaseDatabase.getInstance().getReference("Locations/Camping/")
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (snap in dataSnapshot.children) {
val data = dataSnapshot.value
val newData = mapOf("Recommendations/${dataSnapshot.key}" to data)
FirebaseDatabase.getInstance().getReference("users")
.child(currentuid).updateChildren(newData)
}
}
override fun onCancelled(databaseError: DatabaseError) {
// Handle error
}
})
}
if (valueAsString.contains("Hiking")) {
val ref = FirebaseDatabase.getInstance().getReference("Locations/Hiking/")
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (snap in dataSnapshot.children) {
val data = dataSnapshot.value
val newData = mapOf("Recommendations/${dataSnapshot.key}" to data)
FirebaseDatabase.getInstance().getReference("users")
.child(currentuid).updateChildren(newData)
}
}
override fun onCancelled(databaseError: DatabaseError) {
// Handle error
}
})
}
}
override fun onCancelled(error: DatabaseError) {
}
})
What the code above does it checks if caffe's camping and hiking exists in my users/currentuid/hobbies node. If it does it copies the data from Locations and puts it in users/currentuid/Recommendations.
Note: I also have to check for remaining three hobbies. I only put three of them for testing purposes.
Here is also an image to show what I did
After this I only looped through my Recommendations node and added all the locations to my recycler view with this code snippet:
val recommendedLocations = FirebaseDatabase.getInstance().getReference("users/$currentuid/Recommendations")
recommendedLocations.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (parentSnapshot in snapshot.children) {
for (childSnapshot in parentSnapshot.children) {
val recommendItems = childSnapshot.getValue(RecommendationsClass::class.java)
if (recommendItems != null) {
adapter.add(RecommendationsAdapter(recommendItems))
}
}
}
recommendationsRecyclerView.adapter = adapter
}
override fun onCancelled(error: DatabaseError) {
}
})
I'm trying to filter three RecyclerViews on an activity based on a property that children have in one of my RTDB references have. On my ServiceActivity, I have the three RecyclerViews and I need help in knowing what I need to do to perform the filters. Do I need to create an empty ArrayList for each category property name and then perform the filter function on each? Or how would I perform this? Thank you!
What I Have
A RTDB Reference Child
ServiceActivity.kt
lateinit var autoJobServicesAdapter: JobServicesAdapter
lateinit var homeJobServicesAdapter: JobServicesAdapter
lateinit var personalJobServicesAdapter: JobServicesAdapter
val jobServices = ArrayList<JobService>()
val jobServicesDatabaseRef = FirebaseDatabase.getInstance().reference.child(REF_JOB_SERVICES)
val autoServices = ArrayList<JobService>()
val homeServices = ArrayList<JobService>()
val personalServices = ArrayList<JobService>()
private fun setupAutoRecyclerView() {
autoJobServicesAdapter = JobServicesAdapter(jobServices)
val autoRecyclerView = findViewById<RecyclerView>(R.id.autoServicesRecyclerView)
autoRecyclerView.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
adapter = autoJobServicesAdapter
setHasFixedSize(true)
}
jobServicesDatabaseRef.orderByChild("jobName").addValueEventListener(object: ValueEventListener {
#SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
jobServices.clear()
for (snap in snapshot.children) {
val jobService = JobService(snap.child("category").getValue(String::class.java)!! ,
snap.child("jobName").getValue(String::class.java)!! , snap.child("jobImageUrl").getValue(String::class.java)!! ,
snap.child("jobServiceImageUrl").getValue(String::class.java)!!)
jobServices.add(jobService)
}
autoJobServicesAdapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
Log.d("ServiceActivity", "LoadPost:onCancelled", error.toException())
}
})
}
private fun setupHomeRecyclerView() {
homeJobServicesAdapter = JobServicesAdapter(jobServices)
val homeRecyclerView = findViewById<RecyclerView>(R.id.homeServicesRecyclerView)
homeRecyclerView.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
adapter = homeJobServicesAdapter
setHasFixedSize(true)
}
jobServicesDatabaseRef.orderByChild("jobName").addValueEventListener(object: ValueEventListener {
#SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
jobServices.clear()
for (snap in snapshot.children) {
val jobService = JobService(snap.child("category").getValue(String::class.java)!! ,
snap.child("jobName").getValue(String::class.java)!! , snap.child("jobImageUrl").getValue(String::class.java)!! ,
snap.child("jobServiceImageUrl").getValue(String::class.java)!!)
jobServices.add(jobService)
}
homeJobServicesAdapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
Log.d("ServiceActivity", "LoadPost:onCancelled", error.toException())
}
})
}
private fun setupPersonalRecyclerView() {
personalJobServicesAdapter = JobServicesAdapter(jobServices)
val personalRecyclerView = findViewById<RecyclerView>(R.id.personalServicesRecyclerView)
personalRecyclerView.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
adapter = personalJobServicesAdapter
setHasFixedSize(true)
}
jobServicesDatabaseRef.orderByChild("jobName").addValueEventListener(object: ValueEventListener {
#SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
jobServices.clear()
for (snap in snapshot.children) {
val jobService = JobService(snap.child("category").getValue(String::class.java)!! ,
snap.child("jobName").getValue(String::class.java)!! , snap.child("jobImageUrl").getValue(String::class.java)!! ,
snap.child("jobServiceImageUrl").getValue(String::class.java)!!)
jobServices.add(jobService)
}
personalJobServicesAdapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
Log.d("ServiceActivity", "LoadPost:onCancelled", error.toException())
}
})
}
You're loading the exact same data from Firebase three times. While the client is smart enough to deduplicate these queries for you, it's still better to only process the same data once.
How about creating the three views, list, and adapters all at once, and then loading the data for each with this single snippet? Something like this:
private fun loadDataForRecyclerViews() {
jobServicesDatabaseRef.orderByChild("jobName").addValueEventListener(object: ValueEventListener {
#SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
jobServices.clear()
for (snap in snapshot.children) {
// 👇 Load the category and the job
val category = category
val jobService = (category ,
snap.child("jobName").getValue(String::class.java)!! , snap.child("jobImageUrl").getValue(String::class.java)!! ,
snap.child("jobServiceImageUrl").getValue(String::class.java)!!)
// 👇 Put the job in the correct list based on its category
if (category == "auto") {
autoJobServices.add(jobService)
}
else if (category == "home") {
homeJobServices.add(jobService)
}
else if (category == "personal") {
personalJobServices.add(jobService)
}
}
// 👇 Refresh all views
autoJobServicesAdapter.notifyDataSetChanged()
homeJobServicesAdapter.notifyDataSetChanged()
personalJobServicesAdapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
Log.d("ServiceActivity", "LoadJobs", error.toException())
}
})
}
Note: I did not compile this code, but merely provide it as a starting point. If you get a compiler error, please try to fix it on your own before commenting about it.
How to get data from firebase?
I can implement some data on it, but I do now know why I cannot get and put it in my activity, application.
This is my ValuEventListener, should I use here Livedata?
class FirebaseDB : LiveData<List<Shopping>>() {
var fbItemCount:Long = 0
private val firebaseDB2: FirebaseDatabase = FirebaseDatabase.getInstance()
private val userID = FirebaseAuth.getInstance().uid
public val ref = firebaseDB2.getReference("user/"+userID.toString())
fun removeAll() {
}
fun delete(shopping: Shopping){
}
fun modify(shopping: Shopping) {
}
fun add(shopping: Shopping) {
ref.child(shopping.id.toString()).setValue(shopping)
}
fun getShopping(): List<Shopping> {
val lista: ArrayList<Shopping> = ArrayList()
ref.addValueEventListener(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (messageSnapshot in dataSnapshot.children) {
val shopping: Shopping = Shopping(id = messageSnapshot.child("id").value as Long,
product = messageSnapshot.child("product").value as String,
quantity = messageSnapshot.child("quantity").value as String,
price = messageSnapshot.child("price").value as String,
bought = messageSnapshot.child("bought").value as Boolean)
//Log.i("readDB", "$product $quantity $price $isbought")
lista.add(shopping)
}
}
override fun onCancelled(error: DatabaseError) {
Log.e("MyAdapter", "Failed to delete value.",error.toException())
}
})
fbItemCount = lista.size.toLong()
return lista
}
}
How can i check if it is downloaded somewhere on my app or I just cannot see it or implement well.
EDIT:
I just added log.v... and it starts working...
However after adding a new element in the list, new element is added with double information from firebase for example:
In database I have:
Orange
Apples
I add:
Bananas
Now in application it is like:
Orange
Apples
Orange
Apples
Bananas
onDataChange is an async callback and does not result in the same thread, so return one liveData and observe it, then in the on data change process the list and post it on the liveData.
fun getShopping(): LiveData<List<Shopping>> {
val lista: ArrayList<Shopping> = ArrayList()
val data=MutableLiveData<List<Shopping>>()
ref.addValueEventListener(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (messageSnapshot in dataSnapshot.children) {
val shopping: Shopping = Shopping(id = messageSnapshot.child("id").value as Long,
product = messageSnapshot.child("product").value as String,
quantity = messageSnapshot.child("quantity").value as String,
price = messageSnapshot.child("price").value as String,
bought = messageSnapshot.child("bought").value as Boolean)
//Log.i("readDB", "$product $quantity $price $isbought")
lista.add(shopping)
}
fbItemCount = lista.size.toLong()
data.postValue(lista)
}
override fun onCancelled(error: DatabaseError) {
Log.e("MyAdapter", "Failed to delete value.",error.toException())
}
})
return data
}
About your double items problem this code making it because before making ValueEventListener you created an empty list then after get data from firebase in onDataChange method you add all items that firebase returns then suppose you add a new item by calling 'add' method them onDataChange will be triggered again and giving you again all items with a new one added, but you never remove the old item and add these new items again to this list. That's why it's happening
val lista: ArrayList<Shopping> = ArrayList()
ref.addValueEventListener(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (messageSnapshot in dataSnapshot.children) {
val shopping: Shopping = Shopping(id = messageSnapshot.child("id").value as Long,
product = messageSnapshot.child("product").value as String,
quantity = messageSnapshot.child("quantity").value as String,
price = messageSnapshot.child("price").value as String,
bought = messageSnapshot.child("bought").value as Boolean)
//Log.i("readDB", "$product $quantity $price $isbought")
lista.add(shopping)
}
}
override fun onCancelled(error: DatabaseError) {
Log.e("MyAdapter", "Failed to delete value.",error.toException())
}
})
Correct code would be like this:
var lista = emptyList<Shopping>()
ref.addValueEventListener(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
lista = emptyList()
for (messageSnapshot in dataSnapshot.children) {
val shopping: Shopping = Shopping(id = messageSnapshot.child("id").value as Long,
product = messageSnapshot.child("product").value as String,
quantity = messageSnapshot.child("quantity").value as String,
price = messageSnapshot.child("price").value as String,
bought = messageSnapshot.child("bought").value as Boolean)
//Log.i("readDB", "$product $quantity $price $isbought")
lista.add(shopping)
}
}
override fun onCancelled(error: DatabaseError) {
Log.e("MyAdapter", "Failed to delete value.",error.toException())
}
})
I'm just trying to find an answer how to pass the data from Repository to ViewModel without extra dependencies like RxJava. The LiveData seems as a not good solution here because I don't need to proceed it in my Presentation, only in ViewModel and it's not a good practice to use observeForever.
The code is simple: I use Firebase example trying to pass data with Flow but can't use it within a listener (Suspension functions can be called only within coroutine body error):
Repository
fun fetchFirebaseFlow(): Flow<List<MyData>?> = flow {
var ret: List<MyData>? = null
firebaseDb.child("data").addListenerForSingleValueEvent(
object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val data = dataSnapshot.getValue<List<MyData>>()
emit(data) // Error. How to return the data here?
}
override fun onCancelled(databaseError: DatabaseError) {
emit(databaseError) // Error. How to return the data here?
}
})
// emit(ret) // Useless here
}
ViewModel
private suspend fun fetchFirebase() {
repo.fetchFirebaseFlow().collect { data ->
if (!data.isNullOrEmpty()) {
// Add data to something
} else {
// Something else
}
}
You can use callbackFlow
#ExperimentalCoroutinesApi
fun fetchFirebaseFlow(): Flow<List<String>?> = callbackFlow {
val listener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val data = dataSnapshot.getValue<List<MyData>>()
offer(data)
}
override fun onCancelled(databaseError: DatabaseError) {
}
}
val ref =firebaseDb.child("data")
reef.addListenerForSingleValueEvent(listener)
awaitClose{
//remove listener here
ref.removeEventListener(listener)
}
}
ObservableField is like LiveData but not lifecycle-aware and may be used instead of creating an Observable object.
{
val data = repo.getObservable()
val cb = object : Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(observable: Observable, i: Int) {
observable.removeOnPropertyChangedCallback(this)
val neededData = (observable as ObservableField<*>).get()
}
}
data.addOnPropertyChangedCallback(cb)
}
fun getObservable(): ObservableField<List<MyData>> {
val ret = ObservableField<List<MyData>>()
firebaseDb.child("events").addListenerForSingleValueEvent(
object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
ret.set(dataSnapshot.getValue<List<MyData>>())
}
override fun onCancelled(databaseError: DatabaseError) {
ret.set(null)
}
})
return ret
}
It is also possible to use suspendCancellableCoroutine for a single result. Thanks to Kotlin forum.
private fun getUserInfo() {
val userkey = FirebaseAuth.getInstance().uid ?: ""
val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
ref.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<ViewHolder>()
p0.children.forEach {
Log.d("getUserInfo", it.toString())
val user = it.getValue(User::class.java)
if (user != null) {
adapter.add(UserItem(user))
}
}
UserInfo_RCView.adapter = adapter
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
}
class UserItem(val user: User): Item<ViewHolder>() {
override fun bind(viewHolder: ViewHolder, position: Int) {
viewHolder.itemView.UsernameSettings_txt.text = user.username
Picasso.get().load(user.profileImageUrl).into(viewHolder.itemView.ProfileImage_View)
}
override fun getLayout(): Int {
return R.layout.userinfo
}
}
I wanted to make a snapshot from my current user like this, every time I put the $userkey in my ref value the app crashes with this error:
2018-11-23 22:04:48.414 3871-3871/my.app E/RecyclerView: No adapter attached; skipping layout
2018-11-23 22:04:49.797 3871-3871/my.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: nobrand.ljb.musicshare, PID: 3871
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type myapp.User
When I didn't put the $userkey in my ref value. The app functions as normal put displays all users as expected, but as I said I don't want that.
I'll be thankful for every answer and suggestion!
If you directly access the correct child node, the loop in your code is not needed anymore.
This means there are two simple ways to do this:
Query by key
Remove the loop
I'd recommend removing the loop as there's really no need for it anymore, but wanted to show both options.
Query by key
val ref = FirebaseDatabase.getInstance().getReference("/users")
ref.orderByKey().equalTo(userkey).addListenerForSingleValueEvent(object: ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<ViewHolder>()
p0.children.forEach {
Log.d("getUserInfo", it.toString())
val user = it.getValue(User::class.java)
if (user != null) {
adapter.add(UserItem(user))
}
}
UserInfo_RCView.adapter = adapter
}
override fun onCancelled(p0: DatabaseError) {
throw p0.toException();
}
})
Remove the loop
val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
ref.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<ViewHolder>()
val user = p0.getValue(User::class.java)
if (user != null) {
adapter.add(UserItem(user))
}
UserInfo_RCView.adapter = adapter
}
override fun onCancelled(p0: DatabaseError) {
throw p0.toException();
}
})