1
我必須將驅動程序的當前位置上載到已登錄驅動程序的Uid的busid(「1」或「2」)節點。但是,問題在於,即使Uid存在於節點「2」下,它也總是將該位置上傳到節點「1」。我需要幫助找到哪個節點(「1」或「2」)具有Uid作爲孩子。數據上傳到錯誤的節點。代碼中出現了什麼問題?
int totalbusno = 2;
String driverbusid = "1";
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
busReference = mDatabaseReference.child("Busno");
lat = location.getLatitude();
lon = location.getLongitude();
mUser = FirebaseAuth.getInstance().getCurrentUser();
assert mUser != null;
userid = mUser.getUid();
busReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(int i = 1; i <= totalbusno; i++){
for(DataSnapshot ds : dataSnapshot.getChildren()){
if(ds.child(String.valueOf(i)).hasChild(userid)){
driverbusid = String.valueOf(i);
break;
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Submitdriverlocation submitdriverlocation = new Submitdriverlocation(lat, lon);
mDatabaseReference.child("Busno").child(driverbusid).child(userid).child("location").setValue(submitdriverlocation).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(DriverMapsActivity.this, "Location Updated", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(DriverMapsActivity.this, "Update Failed", Toast.LENGTH_SHORT).show();
}
}
});
但是,如果我不分配一個值出現,應用程序崩潰。即使我在那裏分配一個值,它應該在for循環內改變嗎? –
@AjayReal檢查我的更新答案,如果你不會初始化值,那麼它會因爲空鍵而崩潰 – Rahul
我應該初始化driverbusid爲1嗎? –