有没有办法从Firebase中的查询处理数据

时间:2016-02-23 13:38:33

标签: java android firebase

所以我的问题很简单,但因为我不确定它是否可能。

我只有“MyFirstName”作为我的参考数据;我没有钥匙。 我也不想从查询中获取密钥,因为这将要求我导航到KeyValue数据。但在获取值之前,我需要获取Snapshot值。

在Firebase中我们有这个数据

<!--show.html.erb-->
<!DOCTYPE html>
<html>
<head>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&callback=Init&sensor=false"></script>    <!-- Google Maps API -->
    <script>

    enter code here


    function Init()
    {
    var map;    // Google map object

        // Create a Google coordinate object for where to initially center the map
        var latlng = new google.maps.LatLng( <%= @location.latitude %>,<%= @location.longitude %> );   // Washington, DC

        // Map options for how to display the Google map
        var mapOptions = { zoom: 14, center: latlng  };

        // Show the Google map in the div with the attribute id 'map-canvas'.
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        marker = new google.maps.Marker({
            map:map,
            draggable: true,
            position: latlng,
            title: '<%= @location.address %>',
            animation: google.maps.Animation.DROP,
          });
        marker.setMap(map);

    }

    // Update the Google map for the user's inputted address
    function UpdateMap( )
    {
        var geocoder = new google.maps.Geocoder();    // instantiate a geocoder object

        // Get the user's inputted address
        var address = document.getElementById( "address" ).value;

        // Make asynchronous call to Google geocoding API
        geocoder.geocode( { 'address': address }, function(results, status) {
            var addr_type = results[0].types[0];    // type of address inputted that was geocoded
            if ( status == google.maps.GeocoderStatus.OK ) 
                ShowLocation( results[0].geometry.location, address, addr_type );
            else     
                alert("Geocode was not successful for the following reason: " + status);        
        });
    }
    // Show the location (address) on the map.
    function ShowLocation( latlng, address, addr_type )
    {
        // Center the map at the specified location
        map.setCenter( latlng );

        // Set the zoom level according to the address level of detail the user specified
        var zoom = 12;
        switch ( addr_type )
        {
        case "administrative_area_level_1"  : zoom = 6; break;      // user specified a state
        case "locality"                     : zoom = 10; break;     // user specified a city/town
        case "street_address"               : zoom = 16; break;     // user specified a street address
        }
        map.setZoom( zoom );

        // Place a Google Marker at the same location as the map center 
        // When you hover over the marker, it will display the title
        var marker = new google.maps.Marker( { 
            position: latlng,     
            map: map,      
            title: address
        });

        // Create an InfoWindow for the marker
        var contentString = "" + address + "";  // HTML text to display in the InfoWindow
        var infowindow = new google.maps.InfoWindow( { content: contentString } );

        // Set event to display the InfoWindow anchored to the marker when the marker is clicked.
        google.maps.event.addListener( marker, 'click', function() { infowindow.open( map, marker ); });
    }

    // Call the method 'Init()' to display the google map when the web page is displayed ( load event )
    google.maps.event.addDomListener( window, 'load', Init );


    </script>
    <style>
    /* style settings for Google map */
    #map-canvas
    {
        width : 600px;     /* map width */
        height: 400px;  /* map height */
    }
    </style>
</head>
<body> 
<br>
    <!-- Dislay Google map here -->
    <br/>
   <h5><b>Search:</b></h5>
<div class="row">

        <div class="col-md-5">
        <input type="text" id="address" class="form-control" style="width:480px;"/>
        </div>
    <div class="col-md-4">
        <button onclick="UpdateMap()" class="btn btn-primary">Locate</button>
        </div>
        </div>
        <br>
        <h3><%= @location.address %></h3><h6 id="notice"><%= notice %></h6>
    <div class="row">
    <div class="col-md-7">
<div id='map-canvas'></div>
</div>
<div class="col-md-5">
<h3>Nearby locations</h3>
<ul>
<% for location in @location.nearbys(10) %>
  <li><%= link_to location.address, location %> (<%= location.distance.round(2) %> miles)</li>
<% end %>
<br>
<%= link_to 'Edit', edit_location_path(@location) %> |
<%= link_to 'Back', locations_path %>
</ul>
</div>
<br>

</div>
</body>
</html>

所以在Firebase参考。指向MyData,我们可以这样做。

MyData{
  KeyValue   {
        Name : MyFirstName
        LastName : MyLastName


  }

  KeyValue2 {
        Name : MyFirstName2
        LastName : MyLastName2


  }
}

现在返回将是一个查询。可以从那些调用put,push,甚至setValue吗?

现在自put,push,setvalue不是Query的成员。这是不可能的。

ref.OrderByChild("Name").equalsTo("MyFirstName");

在基础知识中,查询只是一个查询。你无法真正做任何事情。我只想知道是否有可能。

1 个答案:

答案 0 :(得分:0)

不。您必须查看结果并在客户端更新每个结果:

ref.orderByChild("Name").equalTo("MyFirstName").addListenerForSingleValueEvent(new ValueEventListener() {
  public void onDataChange(DataSnapshot snapshot) {
    for (DataSnapshot item: snapshot.getChildren()) {
      item.child("Name").getRef().setValue("Aizen");
    }
  }
  public void onCancelled(FirebaseError firebaseError) {
  }
});

有关查询的详细信息,请参阅retrieving data。有关编写数据的更多信息,请参阅saving data