2017-01-04 89 views

回答

4

這將100%適合你。

ref.child("-K_bWdgeYxYrdmzr67bJ").child("name").addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot snapshot) { 
     try { 
      if (snapshot.getValue() != null) { 
       try { 
        Log.e("TAG", "" + snapshot.getValue()); // your name values you will get here 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("TAG", " it's null."); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onCancelled(FirebaseError firebaseError) { 
     Log.e("onCancelled", " cancelled"); 
    } 
}); 
0

你要監聽器添加到-K_bWdgeYxYrdmzr67bJ的超並從獲得可迭代。它會給你Iterable,然後你可以得到你的價值。

Iterable<DataSnapshot> dsChildData = ds.getChildren(); 

這可以幫助你獲得-K_bWdgeYxYrdmzr67bJ的超級所有對象。

要訪問的經度和緯度,你可以使用這個

for(Datasnapshot dsChild : dsChildData){ 
    MyModel mm = dsChild.getValue(MyModel.class); 
} 

你必須使用ArrayList或列表來存儲你的模型類的所有節點。

0
if (dataSnapshot.exists()) { 
      HashMap<String, Object> hashmap = new HashMap<>(); 
       Iterator<DataSnapshot> friendsiterator = dataSnapshot.getChildren().iterator(); 
       while (friendsiterator.hasNext()) { 

        DataSnapshot bdaySnapShaot = friendsiterator.next(); 

        if (bdaySnapShaot.getKey().equals("latitude")) { 
          hashmap.put("latitude", bdaySnapShaot.getValue().toString()); 
         } else if (bdaySnapShaot.getKey().equals("longitutde")) { 
          hashmap.put("longitutde", bdaySnapShaot.getValue().toString()); 
         } else if (bdaySnapShaot.getKey().equals("name")) { 
          hashmap.put("name", bdaySnapShaot.getValue().toString()); 
         } 


       } 


      } 
+0

創建就像ArrayList的一個的ArrayList類型HashMap中的> mBlockedFriendslist;並將哈希表對象添加到該列表中並訪問該列表。 –

0
mDatabaseReport.getRef().addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        for (DataSnapshot postSnapshot :dataSnapshot.getChildren()) { 

        TextView Push_Key = (TextView)findViewById(R.id.txt_points); 
        Push_Key.setText("" + postSnapshot.getKey()); 
        } 
       } 
       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 
+0

爲我工作獲取指定的子按鍵 –