Hello i am still newbie in Firebase, how can i call the inner field latitude
and ongitude
field? Does me do the correct way structure the database?
I've tried read the docs by Google but it still not worked.
//Write a message to the database
public class LocationActivity extends AppCompatActivity {
private static final String TAG = "LocationActivity";
private RecyclerView.LayoutManager layoutManager;
private Query query;
private RecyclerView recyclerView;
public FirebaseRecyclerAdapter<LocationContent,LocationViewHolder> firebaseRecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// Write a message to the database
query = FirebaseDatabase.getInstance()
.getReference()
.child("Locations")
.limitToLast(50);
query.keepSynced(true);
// set up the RecyclerView
recyclerView = findViewById(R.id.myrecyclerview);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FirebaseRecyclerOptions<LocationContent> options =
new FirebaseRecyclerOptions.Builder<LocationContent>()
.setQuery(query, LocationContent.class)
.build();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
LocationContent lc = ds.child("South Bound").getValue(LocationContent.class);
Log.d(TAG,"Error onDataChange");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
firebaseRecyclerAdapter= new FirebaseRecyclerAdapter<LocationContent, LocationViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull LocationViewHolder holder, int position, @NonNull LocationContent model) {
String doubleLatitude = Double.toString(model.getLatitude());
String doubleLongitude = Double.toString(model.getLongitude());
holder.post_name.setText(model.getName());
holder.post_latitude.setText(doubleLatitude);
holder.post_longitude.setText(doubleLongitude);
}
@NonNull
@Override
public LocationViewHolder onCreateViewHolder(@NonNull ViewGroup viewgroup, int i) {
View view = LayoutInflater.from(viewgroup.getContext()).inflate(R.layout.location_row,viewgroup,false);
return new LocationViewHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
protected void onStart(){
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
firebaseRecyclerAdapter.stopListening();
}
and this is my model
public class LocationContent {
private String Name;
private Double Latitude;
private Double Longitude;
public LocationContent(String name, Double latitude, Double longitude) {
Name = name;
Latitude = latitude;
Longitude = longitude;
}
public LocationContent(){
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Double getLatitude() {
return Latitude;
}
public void setLatitude(Double latitude) {
Latitude = latitude;
}
public Double getLongitude() {
return Longitude;
}
public void setLongitude(Double longitude) {
Longitude = longitude;
}
}