【问题标题】:How to retreive all of the data of a specific pushed ID Child from Firebase or, From a infoWindow如何从 Firebase 或从 infoWindow 检索特定推送 ID Child 的所有数据
【发布时间】:2017-05-03 18:43:49
【问题描述】:

我需要从 Push 生成的节点中获取所有存储的数据 这是我的结构:

我所有的节点都向各种 infoWindows 提供数据 像这样:

每个 infoWindow 都包含特定于多个节点的数据,我需要从每个 infoWindows 或特定节点中获取该数据

我需要这些特定数据将它们放入另一个活动中,方法是从节点或 infoWindow 获取数据 我该怎么做?

这是将数据提供给 infoWindow 的代码:

   FirebaseUtils.getPostRef().orderByKey().addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
                        latitud = dataSnapshot.getValue(Post.class).getLatitud();
                        longitud = dataSnapshot.getValue(Post.class).getLongitud();

                        titulo = dataSnapshot.getValue(Post.class).getTitulo();
                        costo = dataSnapshot.getValue(Post.class).getCosto();
                        mes = dataSnapshot.getValue(Post.class).getMes();
                        numeroDia = dataSnapshot.getValue(Post.class).getDia();
                        organizadoPor = dataSnapshot.getValue(Post.class).getOrganizadoPor();
                        descripcion = dataSnapshot.getValue(Post.class).getDescripicion();
                        hora = dataSnapshot.getValue(Post.class).getHora();
                        minutos = dataSnapshot.getValue(Post.class).getMinutos();




                        BitmapDescriptor bm = BitmapDescriptorFactory.fromResource(R.mipmap.m5);
                        LatLng latLng1 = new LatLng(latitud, longitud);
                        mMarker = new MarkerOptions().position(latLng1).title(titulo).snippet(costo).icon(bm);
                        mp.add(mMap.addMarker(mMarker));
   }

我试试这个但它只给了我最后一个 infoWindow 的数据:

  Post mPost = new Post();
  Intent intent =  new Intent(MainActivity.this, OtherActivity.class);
  intent.putExtra("abc", mPost):

其他活动:

  Intent Intent = getIntent();
   mPost = (Post) intent.getSerializableExtra("abc");

【问题讨论】:

    标签: android google-maps firebase firebase-realtime-database


    【解决方案1】:

    将您的代码编辑为如下所示。但您需要将数据设置到您的post 对象中。您将使用 mMarker.setTag(post) 将您的帖子数据附加到您的标记中;欲了解更多信息,请查看this 链接。

       FirebaseUtils.getPostRef().orderByKey().addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
                            latitud = dataSnapshot.getValue(Post.class).getLatitud();
                            longitud = dataSnapshot.getValue(Post.class).getLongitud();
    
                            titulo = dataSnapshot.getValue(Post.class).getTitulo();
                            costo = dataSnapshot.getValue(Post.class).getCosto();
                            mes = dataSnapshot.getValue(Post.class).getMes();
                            numeroDia = dataSnapshot.getValue(Post.class).getDia();
                            organizadoPor = dataSnapshot.getValue(Post.class).getOrganizadoPor();
                            descripcion = dataSnapshot.getValue(Post.class).getDescripicion();
                            hora = dataSnapshot.getValue(Post.class).getHora();
                            minutos = dataSnapshot.getValue(Post.class).getMinutos();
    
                            Post post = new Post();
                            // add all your data to post object e.g post.setLatitud(latitud);
    
                            BitmapDescriptor bm = BitmapDescriptorFactory.fromResource(R.mipmap.m5);
                            LatLng latLng1 = new LatLng(latitud, longitud);
                            mMarker = new MarkerOptions().position(latLng1).title(titulo).snippet(costo).icon(bm);
                            mp.add(mMap.addMarker(mMarker));
    
                            // Associate your post data with the marker
                            mMarker.setTag(post);
    
    
                            }
                    });
    

    使用此方法检测标记点击

    private void onMarkerClicked() {
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Intent intent = new Intent(HomeActivity.this, OtherActivity.class);
                Bundle bundle = new Bundle();
                // get the data from clicked marker and attach it with the intent 
                bundle.putSerializable("post", (Post) marker.getTag());
                intent.putExtras(bundle);
                startActivity(intent);
                return true;
            }
        });
    }
    

    在您的 OtherActivity 中使用它来获取数据。

      Post mPost = (Post) getIntent().getSerializableExtra("post");
    

    您的 Post 模型可能是这样的。

    public class Post implements Serializable{
    
       private String latitud,longitud,costo,titulo,mes,numeroDia,organizadoPor,descripcion,hora,minutos ;
    
    
    public String getLatitud() {
        return latitud;
    }
    
    public void setLatitud(String latitud) {
        this.latitud = latitud;
    }
    
    public String getLongitud() {
        return longitud;
    }
    
    public void setLongitud(String longitud) {
        this.longitud = longitud;
    }
    
    public String getCosto() {
        return costo;
    }
    
    public void setCosto(String costo) {
        this.costo = costo;
    }
    
    public String getTitulo() {
        return titulo;
    }
    
    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }
    
    public String getMes() {
        return mes;
    }
    
    public void setMes(String mes) {
        this.mes = mes;
    }
    
    public String getNumeroDia() {
        return numeroDia;
    }
    
    public void setNumeroDia(String numeroDia) {
        this.numeroDia = numeroDia;
    }
    
    public String getOrganizadoPor() {
        return organizadoPor;
    }
    
    public void setOrganizadoPor(String organizadoPor) {
        this.organizadoPor = organizadoPor;
    }
    
    public String getDescripcion() {
        return descripcion;
    }
    
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    
    public String getHora() {
        return hora;
    }
    
    public void setHora(String hora) {
        this.hora = hora;
    }
    
    public String getMinutos() {
        return minutos;
    }
    
    public void setMinutos(String minutos) {
        this.minutos = minutos;
    }
     }
    

    【讨论】:

    • 不起作用,这给了我最终的 Post 数据对象,我需要特定的数据
    • 你想如何过滤你的节点?
    • 我需要点击一个包含来自 firebase 的数据的标记,该特定标记的数据被转移到另一个活动,但我不知道该怎么做
    • 啊哈,我终于明白你的问题了。我会为你编辑我的答案:)
    • 还是不行,点击给我最后一个mPost“标签”的数据,一个标记的标题是“A”,另一个标记的标题是“B”当我点击标记“A”在活动中,它给了我最后一个标签的标题“B”我需要“特定标题”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2022-01-21
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多