从 firebase 获取最后 child
Get last child from firebase
我正在尝试向我的 firebase 添加游乐设施,我正在使用以下代码:
dbref = FirebaseDatabase.getInstance().getReference("Users")
databaseQuery = myRef.orderByKey().limitToLast(1)
firebaseAuth = FirebaseAuth.getInstance()
databaseQuery.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
dbref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val user = userSnapshot.getValue(User::class.java)
if (user?.userId == firebaseAuth.currentUser?.uid) {
var user = user?.userName
button = findViewById(R.id.ButtonAdicionaBoleia)
button.setOnClickListener {
var id = BuleiaId+1
var rideDay = findViewById<EditText>(R.id.rideDay).text
var rideTime = findViewById<EditText>(R.id.rideTime).text
var driveFrom = findViewById<EditText>(R.id.driveFrom).text
var driveTo = findViewById<EditText>(R.id.driveTo).text
var pick1 = findViewById<EditText>(R.id.pickUp_1).text
var pick2 = findViewById<EditText>(R.id.pickUp_2).text
var pick3 = findViewById<EditText>(R.id.pickUp_3).text
/*val sharedPref = this.getPreferences(Context.MODE_PRIVATE)
val user_id = sharedPref.getString("user_id", "anonimo")*/
//var user = FirebaseAuth.getInstance().currentUser?.uid
val buleia = Buleia(
id,
rideDay.toString(),
rideTime.toString(),
driveFrom.toString(),
driveTo.toString(),
pick1.toString(),
pick2.toString(),
pick3.toString(),
user.toString()
)
myRef.child(id.toString()).setValue(buleia)
buleiaCreated()
// myRef.setValue(buleia)
}
}
}
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
如何将值传递给第二个 firebase 请求?或者将两者合并在一起?我需要 table 用户的用户名,但我需要 table Buleia 的 child。当前的问题是,在创建新值时,Buleia 正在写入当前值,我想检查数据库中已创建的最后一个 ID,以便我可以创建一个不同的 ID
dbref = FirebaseDatabase.getInstance().getReference("Users")
databaseQuery = myRef.orderByKey().limitToLast(1)
firebaseAuth = FirebaseAuth.getInstance()
databaseQuery.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
}
dbref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val user = userSnapshot.getValue(User::class.java)
if (user?.userId == firebaseAuth.currentUser?.uid) {
var user = user?.userName
button = findViewById(R.id.ButtonAdicionaBoleia)
button.setOnClickListener {
var id = BuleiaId+1
var rideDay = findViewById<EditText>(R.id.rideDay).text
var rideTime = findViewById<EditText>(R.id.rideTime).text
var driveFrom = findViewById<EditText>(R.id.driveFrom).text
var driveTo = findViewById<EditText>(R.id.driveTo).text
var pick1 = findViewById<EditText>(R.id.pickUp_1).text
var pick2 = findViewById<EditText>(R.id.pickUp_2).text
var pick3 = findViewById<EditText>(R.id.pickUp_3).text
/*val sharedPref = this.getPreferences(Context.MODE_PRIVATE)
val user_id = sharedPref.getString("user_id", "anonimo")*/
//var user = FirebaseAuth.getInstance().currentUser?.uid
val buleia = Buleia(
id,
rideDay.toString(),
rideTime.toString(),
driveFrom.toString(),
driveTo.toString(),
pick1.toString(),
pick2.toString(),
pick3.toString(),
user.toString()
)
myRef.child(id.toString()).setValue(buleia)
buleiaCreated()
// myRef.setValue(buleia)
}
}
}
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
我不知道你的逻辑,但这就是我要告诉你的
首先,确保您为 BuleiaId
变量使用正确的范围。其次,您可以为单次提取方案调用 get
或 addListenerForSingleValueEvent
,然后您可以通过嵌套到第一个查询来添加第二个查询。
databaseQuery.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
//your second query with BuleiaId
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
使用 get() :
databaseQuery.get().addOnSuccessListener{
snapshot ->
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
//your second query with BuleiaId
}
}
如果符合您的逻辑,您还可以使用自动生成 120 位唯一 ID 的 push()。
我正在尝试向我的 firebase 添加游乐设施,我正在使用以下代码:
dbref = FirebaseDatabase.getInstance().getReference("Users")
databaseQuery = myRef.orderByKey().limitToLast(1)
firebaseAuth = FirebaseAuth.getInstance()
databaseQuery.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
dbref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val user = userSnapshot.getValue(User::class.java)
if (user?.userId == firebaseAuth.currentUser?.uid) {
var user = user?.userName
button = findViewById(R.id.ButtonAdicionaBoleia)
button.setOnClickListener {
var id = BuleiaId+1
var rideDay = findViewById<EditText>(R.id.rideDay).text
var rideTime = findViewById<EditText>(R.id.rideTime).text
var driveFrom = findViewById<EditText>(R.id.driveFrom).text
var driveTo = findViewById<EditText>(R.id.driveTo).text
var pick1 = findViewById<EditText>(R.id.pickUp_1).text
var pick2 = findViewById<EditText>(R.id.pickUp_2).text
var pick3 = findViewById<EditText>(R.id.pickUp_3).text
/*val sharedPref = this.getPreferences(Context.MODE_PRIVATE)
val user_id = sharedPref.getString("user_id", "anonimo")*/
//var user = FirebaseAuth.getInstance().currentUser?.uid
val buleia = Buleia(
id,
rideDay.toString(),
rideTime.toString(),
driveFrom.toString(),
driveTo.toString(),
pick1.toString(),
pick2.toString(),
pick3.toString(),
user.toString()
)
myRef.child(id.toString()).setValue(buleia)
buleiaCreated()
// myRef.setValue(buleia)
}
}
}
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
如何将值传递给第二个 firebase 请求?或者将两者合并在一起?我需要 table 用户的用户名,但我需要 table Buleia 的 child。当前的问题是,在创建新值时,Buleia 正在写入当前值,我想检查数据库中已创建的最后一个 ID,以便我可以创建一个不同的 ID
dbref = FirebaseDatabase.getInstance().getReference("Users")
databaseQuery = myRef.orderByKey().limitToLast(1)
firebaseAuth = FirebaseAuth.getInstance()
databaseQuery.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
}
dbref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val user = userSnapshot.getValue(User::class.java)
if (user?.userId == firebaseAuth.currentUser?.uid) {
var user = user?.userName
button = findViewById(R.id.ButtonAdicionaBoleia)
button.setOnClickListener {
var id = BuleiaId+1
var rideDay = findViewById<EditText>(R.id.rideDay).text
var rideTime = findViewById<EditText>(R.id.rideTime).text
var driveFrom = findViewById<EditText>(R.id.driveFrom).text
var driveTo = findViewById<EditText>(R.id.driveTo).text
var pick1 = findViewById<EditText>(R.id.pickUp_1).text
var pick2 = findViewById<EditText>(R.id.pickUp_2).text
var pick3 = findViewById<EditText>(R.id.pickUp_3).text
/*val sharedPref = this.getPreferences(Context.MODE_PRIVATE)
val user_id = sharedPref.getString("user_id", "anonimo")*/
//var user = FirebaseAuth.getInstance().currentUser?.uid
val buleia = Buleia(
id,
rideDay.toString(),
rideTime.toString(),
driveFrom.toString(),
driveTo.toString(),
pick1.toString(),
pick2.toString(),
pick3.toString(),
user.toString()
)
myRef.child(id.toString()).setValue(buleia)
buleiaCreated()
// myRef.setValue(buleia)
}
}
}
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
我不知道你的逻辑,但这就是我要告诉你的
首先,确保您为 BuleiaId
变量使用正确的范围。其次,您可以为单次提取方案调用 get
或 addListenerForSingleValueEvent
,然后您可以通过嵌套到第一个查询来添加第二个查询。
databaseQuery.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
//your second query with BuleiaId
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
使用 get() :
databaseQuery.get().addOnSuccessListener{
snapshot ->
var BuleiaId = 0
for (snapshot in snapshot.children){
var viagem = snapshot.getValue(Buleia::class.java)
BuleiaId = viagem?.id!!
//your second query with BuleiaId
}
}
如果符合您的逻辑,您还可以使用自动生成 120 位唯一 ID 的 push()。