我的代码中有一个无限循环。在我完全关闭程序之前,它会不断将数据写入我的Firebase数据库......我真的需要,请帮助。我希望我的函数MemberAdding在单击按钮后执行一次...
override fun MemberAdding(accountKey: String) {
DBdatabaseName!!.reference!!.child(user!!.currentUser!!.uid).child("Group-List").addValueEventListener(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
groupKey = intent.getStringExtra("GroupKey")
val flag = dataSnapshot.child("Groups").child(groupKey).child("members").value.toString()
val newpath = "member" + flag
DBdatabaseName!!.reference!!.child(user!!.currentUser!!.uid).child("Group-List").child("Groups").child(groupKey).child(newpath).setValue(accountKey)
}
override fun onCancelled(databaseError: DatabaseError) {
}
})
DBdatabaseName!!.reference!!.child(user!!.currentUser!!.uid).child("Group-List").addValueEventListener(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
val groupKey = intent.getStringExtra("GroupKey")
var flag = dataSnapshot.child("Groups").child(groupKey).child("members").value.toString()
val flag2 = flag.toInt()
val flag3 = flag2 + 1
DBdatabaseName!!.reference!!.child(user!!.currentUser!!.uid).child("Group-List").child("Groups").child(groupKey).child("members").setValue(flag3)
}
override fun onCancelled(databaseError: DatabaseError) {
}
})
refresh()
}
private fun refresh() {
groupKey = intent.getStringExtra("GroupKey")
val intent = Intent(this@GroupMemberAddingActivity, GroupMemberAddingActivity::class.java)
intent.putExtra("GroupKey", groupKey)
startActivity(intent)
}
}
在下面我还将发送我正在使用的Adapter类
class GroupMemberAddingItemAdapter (context: Context, friendItemList: MutableList<FriendItem>) : BaseAdapter() {
private class ListRowHolder (row: View? ) {
val label: TextView = row!!.findViewById(R.id.friendName_textview) as TextView
val addMember: Button? = row!!.findViewById(R.id.addMember_button) as Button
}
private val mInflater: LayoutInflater = LayoutInflater.from(context)
private var itemList = friendItemList
private var mView: View? = null
private var rowListener: GroupMemberAddingRowListener = context as GroupMemberAddingRowListener
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val friendName: String = itemList.get(position).friendName as String
val friendLastName: String = itemList.get(position).friendLastName as String
val friendemail: String = itemList.get(position).accountKey as String
val vh: ListRowHolder
if (convertView == null) {
mView = mInflater.inflate(R.layout.groupmemberadding_row, parent, false)
vh = ListRowHolder(mView)
mView!!.tag = vh
} else {
mView = convertView
vh = mView!!.tag as ListRowHolder
}
vh.label.text = "$friendName $friendLastName"
vh.addMember!!.setOnClickListener{rowListener.MemberAdding(friendemail)}
return mView!!
}
override fun getItem(index: Int): Any {
return itemList[index]
}
override fun getItemId(index: Int): Long {
return index.toLong()
}
override fun getCount(): Int {
return itemList.size
}
}
我的初始行侦听器是.....
interface GroupMemberAddingRowListener {
fun MemberAdding (accountKey:String)
}
如果您需要更多代码部分,请告诉我....感谢您的帮助
答案 0 :(得分:1)
首先,您必须调试并查看它发生的位置,无论如何我认为它是在onDataChanged
实现中
自从你解雇:
DBdatabaseName!!.reference!!.child(user!!.currentUser!!.uid).child("Group-List").child("Groups").child(groupKey).child("members").setValue(flag3)
setValue将再次触发onDataChanged。
答案 1 :(得分:0)
我发现了问题
DBdatabaseName!!.reference!!.child(user!!.currentUser!!.uid).child("Group-List").addValueEventListener(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
每次数据发生变化时都会调用行调用onDataChange ...(罪魁祸首是addValueEventListener)所以我将其更改为
BdatabaseName!!.reference!!.child(user!!.currentUser!!.uid).child("Group-List").addListenerForSingleValueEvent(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
addListenerForSingleValueEvent仅调用onDataChange一次,而不是销毁所有回调