private fun attemptAdd(){
var focus: View? = null
var cancel: Boolean = false
var prefix: String = prePrefView.text.toString() + prefText.text.toString()
var sp: SimPrefix
//checks for the length of the input
if(prefText.length()<2){
prefText.error = "invalid length"
cancel = true
focus = prefText
}
mDatabaseReference.child("simnumbers")
.orderByChild("simPref")
.equalTo(prefix)
.addListenerForSingleValueEvent(object : ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
dataSnapshot.children.forEach {
sp = it.getValue(SimPrefix::class.java)!!
if(sp.simCard.equals(prefix)){ //checks if there is already a node with the same data
prefText.error = "already exists"
cancel = true
focus = prefText
}
}
}
override fun onCancelled(p0: DatabaseError?) {
}
})
if(cancel){
focus?.requestFocus()
}
else{ //if all is valid, proceed to the next method.
addPrefix()
}
}
我在这里假设的是,它似乎始终不会进入监听器
前进到允许用户输入相同的现有数据的下一个方法。
最佳答案
我提供了可以更改为kotlin的Java代码,还可以将条件添加到onDatachange方法中。
Query query1=reference.child(reference.getKey()).child("simnumbers").orderByChild("simPref").equalTo(prefix);
query1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// here place your condition
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});