我有客戶數據以我的firebase
數據庫中的以下格式存儲。在關鍵火力點獲取關鍵字
{
"fEYfwd8p9oSGAF6iUtXDLkMBCqc2" : { //user id
"-KdLAGG-8VOMd62Noyc8" : { //customer id
"customerCode" : "shi",
"customerLimit" : "2569",
"customerName" : "Shishir"
},
"-KdOraGISFJ6epjWucfh" : { //customer id
//other cust details
}
},
"jxW41BgSNWdRPyWtUkZA2G0eLhf2" : { //user id
"-KdCgJKh6_Rb8MOi-fIj" : {
"customerCode" : "Kau",
"customerLimit" : "800",
"customerName" : "Kaushik"
},
"-KdCgacTYBkThnVWe4sb" : {
//other cust details
},
"-KdCggxld52mq0DsGRjH" : {
//other cust details
},
"-KdChbdoV7nxwZoGLWsY" : {
//other cust details
}
}
}
在我的Android應用程序正在試圖檢查爲客戶創造其他細節之前,是否存在客戶代碼和下面是如何我做這件事。
DatabaseReference dbRef=FirebaseDatabase.getInstance().getReference("customers")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
dbRef.orderByChild("customerCode").equalTo(code).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (!snapshot.exists()) {
editTextAC.setText("");
editTextAC.setError("Customer code not found in database.");
} else {
//need to get customer id here.
}
}
@Override
public void onCancelled(DatabaseError dbError) {
Toast.makeText(activity, dbError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
我以下面的格式獲取快照中的數據。
{
key = jxW41BgSNWasdsaRPsdyaWtUkZA2G0eLhf2,
value = {-KdChbdoV7nfdasxwZoGLWsY = {
customerCode = Ses,
customerLimit = 78888,
customerName = Sheshu
}
}
}
我需要從value
以上獲得客戶ID。我試着做snapshot.getKey()
,它檢索第一級key
,即jxW41BgSNWasdsaRPsdyaWtUkZA2G0eLhf2
,當我說snapshot.getValue()
將檢索value
作爲字符串。我將如何從上面獲取客戶ID?
嘗試'爲(字符串客戶ID:snapshot.getValue()){// 打印客戶ID }'。我可以用iOS來做,但我不確定它是否適用於android –