2017-04-08 92 views
2
{ 
    "DIV_1" : { 
    "ACTINACT" : 1, 
    "COORDINATOR" : "10CA056", 
    "DIV_CODE" : "BSL", 
    "DIV_ID" : 1, 
    "DIV_NAME" : "Bhusawal", 
    "ERP_LOC_CODE" : "CRB", 
    "MTIME" : "2017-04-08T11:02:59", 
    "ZONE_ID" : "ZONE_1" 
    }, 

    "DIV_10" : { 
    "ACTINACT" : 1, 
    "COORDINATOR" : "06CS011", 
    "DIV_CODE" : "UMB", 
    "DIV_ID" : 10, 
    "DIV_NAME" : "Ambala", 
    "ERP_LOC_CODE" : "NRA", 
    "MTIME" : "2017-04-08T11:02:59", 
    "ZONE_ID" : "ZONE_3" 
    } 
} 

如何獲取DIV_ID? 最初我需要比較關鍵(Ex :: Here DIV_10),然後需要獲取DIV_NAME。 在此先感謝...如何從firebase數據中獲取特定的鍵值?

回答

4

我假設DIV_1和DIV_10的父級是根。如果你有一個像「1」或「10」 DIV_ID價值和需要得到的DIV_NAME值,那麼你應該做這樣的:

int divId = 1; // sample 
FirebaseDatabase.getInstance().getReference().orderByChild("DIV_ID").equalTo(divId) 
    .addValueEventListener(new ValueEventListener() { 
     ... onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
       String divName = snapshot.child("DIV_NAME").getValue(String.class); 
       // there you go 
       // and please check if you have more than 1 value as result 
      } 
     } 
     ... 
    } 

但是,如果你有一個像DIV_1DIV_10鍵值,那麼它應該要容易得多。就像這樣:

String key = "DIV_1"; 
FirebaseDatabase.getInstance().getReference(key) 
    .addValueEventListener(new ValueEventListener() { 
     ... onDataChange(DataSnapshot dataSnapshot) { 
      String divName = snapshot.child("DIV_NAME").getValue(String.class); 
      // note that in this sample, it doesn't need to loop, because: 
      // data you get here is one level deeper than data you got on first sample 
     } 
     ... 
    } 

希望這有助於

+0

仍然沒有工作。在這裏divName給空值。 – Surekha

+0

您使用第一個樣品還是第二個?並且是DIV_1和DIV_10的父級確實是root? – koceeng

+0

提供空值的第二個示例 – Surekha

相關問題