I want to take datas from 2 different data collections and unite them under one class. My first collection's name is "Posts" and the other one is "UserDetails". I want to take "downloadUrl" and "comment" from Posts and "username" from UserDetails. But I can't do this. Here is my code:
class FeedsActivity : AppCompatActivity() {
private lateinit var binding : ActivityFeedsBinding
private lateinit var auth: FirebaseAuth
private lateinit var db : FirebaseFirestore
private lateinit var postArrayList : ArrayList<Post>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityFeedsBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.recyclerView
auth = Firebase.auth
db = Firebase.firestore
postArrayList = ArrayList<Post>()
getData()
}
private fun getData(){
db.collection("Posts").addSnapshotListener { value, error ->
if(error != null){
Toast.makeText(this,error.localizedMessage,Toast.LENGTH_LONG).show()
}else{
if(value != null){
if(!value.isEmpty){ //is not empty
val documents = value.documents
for(document in documents){
//casting
val comment = document.get("comment") as String
val downloadUrl = document.get("downloadUrl") as String
val post = Post(comment,downloadUrl)
postArrayList.add(post)
}
}
}
}
}
db.collection("UserDetails").addSnapshotListener { value, error ->
if(error != null){
Toast.makeText(this,error.localizedMessage,Toast.LENGTH_LONG).show()
}else{
if(value != null){
if(!value.isEmpty){ //is not empty
val documents = value.documents
for(document in documents){
//casting
val username = document.get("username") as String
val post = Post(username,comment,downloadUrl)
postArrayList.add(post)
}
}
}
}
}
}
How can I make this happen?