您好,我仍然是Firebase的新手,如何称呼内部字段latitude
和ongitude
字段?我是否以正确的方式构建数据库?
我尝试阅读Google的文档,但仍然无法正常工作。
//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();
}
这是我的模特
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;
}
}
答案 0 :(得分:2)
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationsRef = rootRef.child("Locations");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
double lat = ds.child("South Bound").child("Latitude").getValue(Double.class);
double lng = ds.child("South Bound").child("Longitude").getValue(Double.class);
Log.d(TAG, lat, ", " + lng);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
locationsRef.addListenerForSingleValueEvent(valueEventListener);
或使用您的LocationContent
类:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationsRef = rootRef.child("Locations");
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, lc.getLatitude(), ", " + lc.getLongitude());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
locationsRef.addListenerForSingleValueEvent(valueEventListener);
如果您打算使用第二种解决方案,请还注意数据库中字段的名称与LocationContent
类中字段的名称。因此,请同时从以下帖子中查看我的答案:
编辑:
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;
}