0
Firebase 데이터베이스에서 문제 목록을 얻고 LiveData
을 반환합니다. 코드 내 활동의LiveData Obserever는 첫 번째 값만 잡습니다.
fun getAllIssues(): MutableLiveData<IssueEntity> {
val issues: MutableLiveData<IssueEntity> = MutableLiveData()
val issuesReference = FirebaseDatabase.getInstance().reference.child("issues")
issuesReference.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError?) {
}
override fun onDataChange(snapshot: DataSnapshot?) {
if (snapshot == null) {
return
}
snapshot.children.forEach({
val issueMessage = it.child("message").value.toString()
val issueTitle = it.child("title").value.toString()
val issueOwner = it.child("owner").value.toString()
issues.postValue(IssueEntity(issueTitle, issueMessage, issueOwner))
})
}
})
return issues
}
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_all_issues)
val issues: MutableList<IssueEntity> = mutableListOf()
issuesList.adapter = IssuesAdapter(issues)
IssueRepository().getAllIssues().observe(this, Observer {
if (it != null) {
issues.add(it)
val issuesAdapter = issuesList.adapter as IssuesAdapter
issuesAdapter.notifyDataSetChanged()
}
})
}
그러나 관찰자는 첫 번째 값을 가져옵니다. 이유를 말해 주실 수 있습니까?