android studio中的Firebase数据库:如何从子级获取数据

huangapple go评论80阅读模式
英文:

android studio firebase database: how to get data from sub child

问题

以下是翻译好的内容:

我正在尝试从我的Firebase数据库检索减速带数据,然后显示从数据库中检索到的带有信息的减速带。

问题在于我的数据库包含子级,而这些子级的名称是基于位置的,因此无法通过子级名称检索数据。

所以是否有一种方法可以检索所有子级名称?这样我就可以通过检索子级的数据。

这是我的数据库:

android studio中的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

android studio中的Firebase数据库:如何从子级获取数据

this is my code to retrieve the data

     private void showbumps() {
ref =FirebaseDatabase.getInstance().getReference().child(&quot;SpeedBump&quot;);
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                List&lt;SpeedBump&gt; bumps = new ArrayList&lt;&gt;();
                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 = &quot; type : &quot;+ bump_type + &quot; size : &quot;+bump_size ;
// set height &amp; 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(&quot;Bump info&quot;).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(&quot;SpeedBump&quot;);
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            List&lt;SpeedBump&gt; bumps = new ArrayList&lt;&gt;();
            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.

huangapple
  • 本文由 发表于 2020年10月22日 22:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64484252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定