我正在创建一个屏幕,需要从Firebase实时数据库检索数据并将其显示在recyclerview中。如果我尝试打开活动,则不会加载或显示数据,但是如果我再次打开活动,则数据将正确显示。我相信我也无法写入数据库
活动:
class SelectCollaborators : AppCompatActivity() {
private val listaIdsCollaboratorsSelecionar = ArrayList<Collaborator>()
private var collaboratorsRef: DatabaseReference = FirebaseDatabase.getInstance().reference
.child("collaborators")
.child(UsuarioFirebase.getIdentificadorUsuario())//get current user id
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_selecionar_membros)
Log.i("selecionar","REF:" + collaboratorsRef.toString())
iniciarToolbar()
iniciarRecyclerView()
listarCollaboratorsParaSelecionar()
fabSelecionarMembros.setOnClickListener {
startActivity(Intent(applicationContext, DetalhesGrupoActivity::class.java))
}
Toast.makeText(applicationContext,"onCreate",Toast.LENGTH_SHORT).show()
}
private fun iniciarRecyclerView(){
recyclerSelecionarMembros.setLayoutManager(LinearLayoutManager(applicationContext))
recyclerSelecionarMembros.setHasFixedSize(true)
}
private fun listarCollaboratorsParaSelecionar() {
listaIdsCollaboratorsSelecionar.clear()
val postListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
// Get Post object and use the values to update the
for(ds in dataSnapshot.children){
val collaborator = ds.getValue(Collaborators::class.java)
//reperarDadosUsuarioAtualizado(collaborator!!.id)
listaIdsCollaboratorsSelecionar.add(collaborator!!)
Log.i("selecionar","NOME:" + collaborator.nomeExibicao)
}
recyclerSelecionarMembros.adapter = AdapterSelecionarMembros(listaIdsCollaboratorsSelecionar,applicationContext)
recyclerSelecionarMembros.adapter!!.notifyDataSetChanged()
}
override fun onCancelled(databaseError: DatabaseError) {
// Getting Post failed, log a message
Log.i("selecionar", "loadPost:onCancelled", databaseError.toException())
// ...
}
}
collaboratorsRef.addValueEventListener(postListener)
}
private fun iniciarToolbar(){
val toolbar:Toolbar = findViewById(R.id.toolbarSelecionarMembros)
setSupportActionBar(toolbar)
getSupportActionBar()!!.setDisplayHomeAsUpEnabled(true)
supportActionBar!!.title = resources.getString(R.string.titleActivityAdicionarMembros)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuInflater = menuInflater
menuInflater.inflate(R.menu.menu_pesquisa, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return super.onOptionsItemSelected(item)
}
override fun onStart() {
super.onStart()
Toast.makeText(applicationContext,"onStart",Toast.LENGTH_SHORT).show()
}
override fun onResume() {
super.onResume()
Toast.makeText(applicationContext,"onResume",Toast.LENGTH_SHORT).show()
}
}
数据库:
"collaborators" : {
"5P6DzPIEuiSKU5UYGo03AwSMTED3" : {
"HeGEtXVmf0btmlg4CGO2s5YZtdQ2" : {
"caminhoFoto" : "https://firebasestorage.googleapis.com/v0...",
"id" : "HeGEtXVmf0btmlg4CGO2s5YZtdQ2",
"nomeExibicao" : "Jane Doe"
"nomePesquisa" : "JANE DOE"
},
"nKTlIyaDc3O3gxNpsz8hwUrbGq53" : {
"caminhoFoto" : "https://firebasestorage.googleapis.com/...",
"id" : "nKTlIyaDc3O3gxNpsz8hwUrbGq53",
"nomeExibicao" : "John Doe",
"nomePesquisa" : "JOHN DOE"
}
},
适配器:(目前仅显示图像,以后将添加其他项目)
class AdapterSelecionarMembros(private val listaCollaborators:List<Collaborators>, private val context: Context) : RecyclerView.Adapter<AdapterSelecionarMembros.ViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
val itemView = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.item_selecionar_membros, viewGroup, false)
return ViewHolder(itemView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val membroId = listaCollaborators[position]
Glide.with(context)
.load(membroId.caminhoFoto)
.into(holder.foto)
}
override fun getItemCount(): Int {
return listaCollaborators.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val foto:RoundedImageView = itemView.findViewById(R.id.imageSelecionarMembro)
val nome:TextView = itemView.findViewById(R.id.textNomeMembroListaSelecionar)
val descricao:TextView = itemView.findViewById(R.id.textDescricaoSelecionarMembro)
val checkBox:CheckBox = itemView.findViewById(R.id.checkboxSelecionarMembro)
}
}
合作者:
@IgnoreExtraProperties
data class Collaborators(
var nomeExibicao: String = "",
var nomePesquisa: String = "",
var caminhoFoto: String = "",
var descricaoPerfil: String = "",
var id: String = "",
var isSelected: Boolean = false):Serializable{
}
为什么仅当我重新打开“活动”时才加载数据? 预先感谢!