通过InfoWindow OnClickListener中的putExtra传递Firebase信息

时间:2017-06-01 05:30:54

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

所以我有这个代码从我的Firebase数据库中获取数据。然后,我把它放在变量中。使用变量制作标记。这些变量也将用于通过onInfoWindowClickListener中的putExtra传递信息。

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int ikonLokasi = 0;
            for(DataSnapshot child : dataSnapshot.child("places").getChildren())
            {
                latitude_ = child.child("latitude").getValue(Double.class);
                longitude_ = child.child("longitude").getValue(Double.class);
                nama_tempat = child.child("nama").getValue(String.class);
                kategori = child.child("kategori").getValue(String.class);
                alamat = child.child("alamat").getValue(String.class);
                harga = child.child("harga").getValue(String.class);
                nomor_telepon = child.child("nomor_telepon").getValue(String.class);

                .
                .
                .

                LatLng latlng = new LatLng(latitude_, longitude_);
                googleMap.addMarker(new MarkerOptions()
                        .position(latlng)
                        .title(nama_tempat)
                        .icon(BitmapDescriptorFactory.fromResource(ikonLokasi))
                        .snippet(alamat));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    .
    .
    .

    googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {
            marker.showInfoWindow();
            return true;
        }
    });

    googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            marker.getTitle();
            Intent mainIntent = new Intent(getActivity(),DetailsActivity.class);
            mainIntent.putExtra("Judul", nama_tempat);
            mainIntent.putExtra("Alamat", alamat);
            mainIntent.putExtra("Harga", harga);
            mainIntent.putExtra("NomorTelepon", nomor_telepon);
            getActivity().startActivity(mainIntent);
        }
    });

现在我的问题是,我点击的每个InfoWindow,信息总是显示我从数据库中提取的最后一个数据。例如,我点击了A的地方标记InfoWindow,但是数据库中最后提取的数据是E的位置标记。因此,当DetailsActivity使用getExtras()时,它会显示E的位置信息,而不是A' s。这也发生在C和D的标记处。

这里是DetailsActivity的代码:

 Intent detailsIntent = getIntent();
    placeTitle.setText(detailsIntent.getStringExtra("Judul"));
    placeAddress.setText(detailsIntent.getStringExtra("Alamat"));
    placeNumber.setText(detailsIntent.getStringExtra("Harga"));
    placePrice.setText(detailsIntent.getStringExtra("NomorTelepon"));

关于如何解决它的任何想法?

1 个答案:

答案 0 :(得分:0)

您正在使用for循环来获取从firebase接收的数据列表,并且您正在将值更新为string和double。因此它将继续替换最后一个值,您的最​​终值将是您将获取的最后一个数据的值。要解决此问题,您可以使用HashMap

首先为标记细节创建一个模型类

public class Detail{
private double latitude;
private double longitude;
private String nama_tempat;
private String kategori;
private String alamat;
private String harga;
private String nomor_telepon;

public Detail(double latitude, double longitude, String nama_tempat, String kategori, String alamat, String harga, String nomor_telepon) {
    this.latitude = latitude;
    this.longitude = longitude;
    this.nama_tempat = nama_tempat;
    this.kategori = kategori;
    this.alamat = alamat;
    this.harga = harga;
    this.nomor_telepon = nomor_telepon;
}

public double getLatitude() {
    return latitude;
}

public void setLatitude(double latitude) {
    this.latitude = latitude;
}

public double getLongitude() {
    return longitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
}

public String getNama_tempat() {
    return nama_tempat;
}

public void setNama_tempat(String nama_tempat) {
    this.nama_tempat = nama_tempat;
}

public String getKategori() {
    return kategori;
}

public void setKategori(String kategori) {
    this.kategori = kategori;
}

public String getAlamat() {
    return alamat;
}

public void setAlamat(String alamat) {
    this.alamat = alamat;
}

public String getHarga() {
    return harga;
}

public void setHarga(String harga) {
    this.harga = harga;
}

public String getNomor_telepon() {
    return nomor_telepon;
}

public void setNomor_telepon(String nomor_telepon) {
    this.nomor_telepon = nomor_telepon;
}

}

在你onDataChange上使用HashMap以Marker为键存储Detail对象。这样,Marker将与该对象链接。

    private HashMap<Marker, Detail> detailMarkerMap;
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int ikonLokasi = 0;
        for(DataSnapshot child : dataSnapshot.child("places").getChildren())
        {
            latitude_ = child.child("latitude").getValue(Double.class);
            longitude_ = child.child("longitude").getValue(Double.class);
            nama_tempat = child.child("nama").getValue(String.class);
            kategori = child.child("kategori").getValue(String.class);
            alamat = child.child("alamat").getValue(String.class);
            harga = child.child("harga").getValue(String.class);
            nomor_telepon = child.child("nomor_telepon").getValue(String.class);

            Detail detail = new Detail(latitude_, longitude_, nama_tempat,   kategori, alamat, harga, nomor_telepon );



            .
            .
            .

            LatLng latlng = new LatLng(latitude_, longitude_);
            Marker marker = googleMap.addMarker(new MarkerOptions()
            .position(latlng)
            .title(nama_tempat)
            .icon(BitmapDescriptorFactory.fromResource(ikonLokasi))
            .snippet(alamat));

            detailMarkerMap.put(marker, detail);
        }
    }

On On OnfoWindowClick按键标记

获取详细信息
@Override
    public void onInfoWindowClick(Marker marker) {
        marker.getTitle();

        Detail detail = detailMarkerMap.get(marker);

        Intent mainIntent = new Intent(getActivity(),DetailsActivity.class);
        mainIntent.putExtra("Judul", detail.getNama_tempat());
        mainIntent.putExtra("Alamat", detail.getAlamat());
        mainIntent.putExtra("Harga", detail.getHarga());
        mainIntent.putExtra("NomorTelepon", detail.getNomor_telepon());
        getActivity().startActivity(mainIntent);
    }