Java android studio firebase数据库:如何从子级获取数据
我试图从firebase数据库中检索凹凸数据,然后显示凹凸及其从数据库中检索到的信息 问题是我的数据库包含子项,而这个子项名称基于位置,因此我无法按子项名称检索数据 那么,有没有一种方法可以检索所有子级名称?所以我可以通过r检索所有数据从子对象检索数据 这是我的数据库 这是我检索数据的代码Java android studio firebase数据库:如何从子级获取数据,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我试图从firebase数据库中检索凹凸数据,然后显示凹凸及其从数据库中检索到的信息 问题是我的数据库包含子项,而这个子项名称基于位置,因此我无法按子项名称检索数据 那么,有没有一种方法可以检索所有子级名称?所以我可以通过r检索所有数据从子对象检索数据 这是我的数据库 这是我检索数据的代码 private void showbumps() { ref =FirebaseDatabase.getInstance().getReference().child("SpeedBum
private void showbumps() {
ref =FirebaseDatabase.getInstance().getReference().child("SpeedBump");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
List<SpeedBump> bumps = new ArrayList<>();
bumps.clear();
// get bumps info from DB
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
SpeedBump bump = postSnapshot.getValue(SpeedBump.class);
bumps.add(bump);
double bump_lat = bump.getLatitude();
double bump_long = bump.getLongitude();
String bump_type = bump.getType();
String bump_size = bump.getSize();
LatLng latLng = new LatLng(bump_lat,bump_long);
String bump_info = " type : "+ bump_type + " size : "+bump_size ;
// set height & width - apply style
int height = 130;
int width = 130;
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.pin);
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
BitmapDescriptor smallMarkerIcon = BitmapDescriptorFactory.fromBitmap(smallMarker);
// BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.pin);
MarkerOptions marker = new MarkerOptions().position(latLng).title("Bump info").snippet(bump_info).icon(smallMarkerIcon);
// create marker for bumps
mMap.addMarker(marker);
} }
private void showbumps(){
ref=FirebaseDatabase.getInstance().getReference().child(“SpeedBump”);
ref.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot snapshot snapshot){
列表凹凸=新建ArrayList();
碰撞。清除();
//从数据库获取凹凸信息
对于(DataSnapshot postSnapshot:snapshot.getChildren()){
SpeedBump=postSnapshot.getValue(SpeedBump.class);
凹凸。添加(凹凸);
double bump_lat=bump.getLatitude();
double bump_long=bump.getLongitude();
字符串bump_type=bump.getType();
字符串bump_size=bump.getSize();
LatLng LatLng=新LatLng(凹凸、凹凸);
字符串bump\u info=“type:”+bump\u type+“size:”+bump\u size;
//设置高度和宽度-应用样式
整数高度=130;
整数宽度=130;
位图b=BitmapFactory.decodeResource(getResources(),R.drawable.pin);
位图smallMarker=Bitmap.createScaledBitmap(b,宽度,高度,false);
BitmapDescriptor smallMarkerIcon=BitmapDescriptorFactory.fromBitmap(smallMarker);
//BitmapDescriptor图标=BitmapDescriptorFactory.fromResource(R.drawable.pin);
MarkerOptions marker=newmarkeroptions().position(latLng).title(“凹凸信息”).snippet(凹凸信息).icon(smallMarkerIcon);
//为凹凸创建标记
mMap.addMarker(marker);
} }
代码没有显示任何凹凸。您就快到了。您只需要一个额外的循环,以满足JSON中未知键的额外级别:
private void showbumps() {
ref =FirebaseDatabase.getInstance().getReference().child("SpeedBump");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
List<SpeedBump> bumps = new ArrayList<>();
bumps.clear();
for (DataSnapshot locationSnapshot: snapshot.getChildren()) {
for (DataSnapshot bumpSnapshot: locationSnapshot.getChildren()) {
SpeedBump bump = bumpSnapshot.getValue(SpeedBump.class);
...
private void showbumps(){
ref=FirebaseDatabase.getInstance().getReference().child(“SpeedBump”);
ref.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot snapshot snapshot){
列表凹凸=新建ArrayList();
碰撞。清除();
对于(DataSnapshot位置快照:snapshot.getChildren()){
对于(DataSnapshot bumpSnapshot:locationSnapshot.getChildren()){
SpeedBump bump=bumpSnapshot.getValue(SpeedBump.class);
...
我发现为快照变量提供清晰的名称最有帮助,这些名称指示它们捕获的数据的哪一部分。如何初始化
ref
ref=FirebaseDatabase.getInstance().getReference().child(“SpeedBump”);