2016-07-25 103 views
2

我的火力地堡數據庫建設的結構檢索數據看起來像這樣:從火力地堡的Android工作室

enter image description here

我想以這樣的方式來從中檢索數據,當我改變「名稱」的價值數據庫會在Android Studio中立即更改。現在,我使用.addChildEventListner方法結合Map<String, String>。有人能幫助我嗎?

編輯:我的代碼:

Firebase markerRef = myFirebaseRef.child("marker"); 

    markerRef.addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) { 
      Map<String, String> map = dataSnapshot.getValue(Map.class); 
      double latitude = Double.parseDouble(map.get("Lat")); 
      double longitude = Double.parseDouble(map.get("Lon")); 
      LatLng location = new LatLng(latitude, longitude); 

      String filename = map.get("Name"); 
      String[] splitString = filename.split(","); 


      mMap.addMarker(new MarkerOptions() 
        .position(location) 
        .title(splitString[0]) 
        .snippet(splitString[1]) 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
     } 

     @Override 
     public void onChildChanged(com.firebase.client.DataSnapshot dataSnapshot, String s) { 


     } 

     @Override 
     public void onChildRemoved(com.firebase.client.DataSnapshot dataSnapshot) { 

     } 

     @Override 
     public void onChildMoved(com.firebase.client.DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onCancelled(FirebaseError firebaseError) { 

     } 
    }); 
+0

顯示代碼,我們不能幫你沒有它 –

+0

@DimaRostopira我添加的代碼。 – Michal

回答

1

在您的活動創建空HashMap Hasmap<String, Marker> markers = new Hashmap<>(); 然後在onChildAdded

Marker m = mMap.addMarker(new MarkerOptions() 
         .position(location) 
         .title(splitString[0]) 
         .snippet(splitString[1]) 
         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
markers.add.put(dataSnapshot.getKey(), m); 

而且在onChildChanged

Marker m = markers.get(dataSnapshot.getKey()); 
m.setPosition(newLocation); 
//And anything else, that changing 

最後在onChildRemo ved

markers.get(dataSnapshot.getKey()).remove(); 
markers.remove(dataSnapshot.getKey()); 
+0

非常感謝。一切都很好。 :) – Michal

+0

@Michal謝謝接受,我終於可以爲我的問題設置賞金:D –

1

通過將異步偵聽器附加到FirebaseDatabase引用來檢索Firebase數據。對於數據的初始狀態,監聽器被觸發一次,並且在數據發生任何變化時再次觸發監聽器。

private DatabaseReference mDatabase; 
mDatabase = FirebaseDatabase.getInstance().getReference(); 

要添加的值事件偵聽器,使用addValueEventListener()或addListenerForSingleValueEvent()方法。要添加子事件偵聽器,請使用addChildEventListener()方法。

可以使用onDataChange()方法讀取給定路徑上內容的靜態快照,因爲它們在事件發生時存在。該方法在監聽器連接時觸發一次,並且每次數據(包括子節點)都會更改。事件回調會傳遞包含該位置所有數據的快照,包括子數據。如果沒有數據,則返回的快照爲空。

實施例: -

ValueEventListener postListener = new ValueEventListener() { 
@Override 
public void onDataChange(DataSnapshot dataSnapshot) { 
    // Get Post object and use the values to update the UI 
    Post post = dataSnapshot.getValue(Post.class); 
    // ... 
} 

@Override 
public void onCancelled(DatabaseError databaseError) { 
    // Getting Post failed, log a message 
    Log.w(TAG, "loadPost:onCancelled", databaseError.toException()); 
    // ... 
} 
}; 
mPostReference.addValueEventListener(postListener); 

兒童事件響應於發生從操作的節點的子節點的特定操作觸發例如一個新的孩子通過push()方法增加或一個孩子是通過updateChildren()方法更新。它們中的每一個都可以用於監聽數據庫中特定節點的更改。

例子: -

ChildEventListener childEventListener = new ChildEventListener() { 
@Override 
public void onChildAdded(DataSnapshot dataSnapshot, String  previousChildName) { 
    Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey()); 

    // A new comment has been added, add it to the displayed list 
    Comment comment = dataSnapshot.getValue(Comment.class); 

    // ... 
} 

@Override 
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) { 
    Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey()); 

    // A comment has changed, use the key to determine if we are displaying this 
    // comment and if so displayed the changed comment. 
    Comment newComment = dataSnapshot.getValue(Comment.class); 
    String commentKey = dataSnapshot.getKey(); 

    // ... 
} 

@Override 
public void onChildRemoved(DataSnapshot dataSnapshot) { 
    Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey()); 

    // A comment has changed, use the key to determine if we are displaying this 
    // comment and if so remove it. 
    String commentKey = dataSnapshot.getKey(); 

    // ... 
} 

@Override 
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) { 
    Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey()); 

    // A comment has changed position, use the key to determine if we are 
    // displaying this comment and if so move it. 
    Comment movedComment = dataSnapshot.getValue(Comment.class); 
    String commentKey = dataSnapshot.getKey(); 

    // ... 
} 

@Override 
public void onCancelled(DatabaseError databaseError) { 
    Log.w(TAG, "postComments:onCancelled", databaseError.toException()); 
    Toast.makeText(mContext, "Failed to load comments.", 
      Toast.LENGTH_SHORT).show(); 
} 
}; 
ref.addChildEventListener(childEventListener); 

在某些情況下,你可能需要一個回調被調用一次,然後立即刪除,例如初始化,你不要指望改變UI元素時。您可以使用addListenerForSingleValueEvent()方法來簡化此方案:它觸發一次,然後不再觸發。

這對於只需要加載一次而且預計不會頻繁更改或需要主動偵聽的數據很有用。例如,

例子: -

final String userId = getUid(); 
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(
    new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      // Get user value 
      User user = dataSnapshot.getValue(User.class); 

      // ... 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.w(TAG, "getUser:onCancelled", databaseError.toException()); 
     } 
    });