index value issue on the markers

2019-08-29 05:22发布

问题:

I want to display these values on markers from the fcmUsers list

when user click on marker it will show its own value or data in text view from the fcmUsers list

fcmUsers List have public Long createdat; and public Long hajjappnum;

DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference().child("GPSLocationHistory").child("DevicesLatest");
UserRef.keepSynced(true);
new GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);


FirebaseDatabase database = FirebaseDatabase.getInstance();

//  myRef = database.getReference("GPSLocationHistory/DevicesLatest");


UserRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        latLngList.clear();
        mMap.clear();
        if (marker != null) {
            marker.remove();
        }
        Toast.makeText(MapsActivity.this, "change", Toast.LENGTH_SHORT).show();
        Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
         fcmUsers = new ArrayList<>();
         i = 0;
        while (dataSnapshots.hasNext()) {
            DataSnapshot dataSnapshotChild = dataSnapshots.next();
            ModelClassFireBase fcmUser = dataSnapshotChild.getValue(ModelClassFireBase.class);
            fcmUsers.add(fcmUser);
            LatLng latLng = new LatLng(Double.parseDouble(fcmUsers.get(i).lati.toString()), Double.parseDouble(fcmUsers.get(i).longi.toString())); // Use your server's methods
            latLngList.add(latLng);
            mark1 = mMap.addMarker(new MarkerOptions().title(fcmUsers.get(i).Battery.toString()).position(latLngList.get(i)));//.icon(BitmapDescriptorFactory.fromResource(R.drawable.female4)));
            // Toast.makeText(getApplicationContext(), uniqueids.get(i).toString(), Toast.LENGTH_LONG).show();
            markerList.add(mark1);


            i++;

            if (fcmUsers.size() > 0) {

            } else {
                // Display dialog for there is no user available.
            }

        }


    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // for handling database error
    }
});
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    @Override
    public View getInfoWindow(Marker arg0) {
        return null;
    }

    // Defines the contents of the InfoWindow
    @Override
    public View getInfoContents(Marker arg0) {

        // Getting view from the layout file info_window_layout
        View v = getLayoutInflater().inflate(R.layout.info_widow_layout, null);

        // Getting the position from the marker
        LatLng latLng = arg0.getPosition();

        // Getting reference to the TextView to set latitude
        TextView userbattery = (TextView) v.findViewById(R.id.tv_lat);

        // Getting reference to the TextView to set longitude
       // TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
       // ImageView userimage = (ImageView) v.findViewById(R.id.userimage);


        userbattery.setText("user battery:" + fcmUsers.get(i-1).Battery.toString());

        return v;

    }

});

回答1:

Firstly, I don't see a reason why you need to store Arraylists off of the Firebase method. I think you should remove them and the i variable.

If you do need lists, they should only be initialized starting at onDataChange

Secondly, your marker currently only displays the battery.

new MarkerOptions().title(fcmUsers.get(i).Battery.toString())

You can make a String variable of the data you want to show, and replace that.


If you want to store the entire object with the Marker, use tags.

final ModelClassFireBase fcmUser = dataSnapshotChild.getValue(ModelClassFireBase.class);
Marker mark = mMap.addMarker(new MarkerOptions()
    .title(user.getBattery())
    .position(new LatLng(user.getLatitude(), user.getLongitude());
mark.setTag(user);

And down below, get the tag

final ModelClassFireBase user = ((ModelClassFireBase) arg0).getTag();
userbattery.setText("user battery:" + user.getBattery());

You cannot use get(i) in this place because that is not the actual position of that marker