我想顯示用戶名,但是面臨索引問題。所有標記都顯示相同的文字。從firebase中獲取數據但面臨索引錯誤
我初始化與0
頂部i
值。我可以做什麼行userbattery.setText("user battery:" + fcmUsers.get(i-1).Battery.toString());
,可以分別顯示所有的標記值?
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;
}
});
不,我不想從列表中獲取最後一個對象我想要當用戶點擊標記時它顯示標記的電池(來自fcUsers列表) 它是一個跟蹤應用 – Ahmad
每個標記具有獨立的價值,我只是想顯示在 – Ahmad
你已經做了'的MarkerOptions()。標題(fcmUsers.get(我)'當你添加的標記標記,那麼爲什麼你認爲你需要從用戶列表中再次獲取它? –