gpt4 book ai didi

java - 如何在 Firebase 中调用内部字段

转载 作者:行者123 更新时间:2023-12-02 01:35:21 24 4
gpt4 key购买 nike

您好,我还是 Firebase 的新手,如何调用内部字段 latitudeongitude 字段?我是否以正确的方式构建数据库?

enter image description here

我尝试阅读 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;
}

}

最佳答案

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;
}

关于java - 如何在 Firebase 中调用内部字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55322395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com