英文:
android studio firebase database: how to get data from sub child
问题
以下是翻译好的内容:
我正在尝试从我的Firebase数据库检索减速带数据,然后显示从数据库中检索到的带有信息的减速带。
问题在于我的数据库包含子级,而这些子级的名称是基于位置的,因此无法通过子级名称检索数据。
所以是否有一种方法可以检索所有子级名称?这样我就可以通过检索子级的数据。
这是我的数据库:
这是我检索数据的代码:
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 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 = "类型:" + bump_type + " 尺寸:" + bump_size ;
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);
MarkerOptions marker = new MarkerOptions().position(latLng).title("减速带信息").snippet(bump_info).icon(smallMarkerIcon);
mMap.addMarker(marker);
}
}
}
}
此代码没有显示任何减速带。
英文:
I am trying to retrieve bumps data from my firebase database, then display the bumps with their information that are retrieved from DB.
the problem is my DB contains sub child, and this sub child name's are based on location so I can't retrieve data by sub child name.
so is there a method to retrieve all sub child names? so I can retrieve all data by r retrieve data from sub child ?
this is my DB
this is my code to retrieve the data
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);
} }
the code doesn't show any bump.
答案1
得分: 0
你已经接近成功了。你只需要额外添加一个循环,以处理 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);
...
我发现为快照变量赋予清晰的名称会更有帮助,这样可以表明它们捕获了数据的哪个部分。
英文:
You're almost there. You just need an extra loop, to cater for the extra level of unknown keys in your 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);
...
I find it most helpful to give the snapshot variables clear names that indicate what part of the data they capture.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论