我试图从显示的图片中读取数据库中的数据。当只有一个孩子延伸时(餐馆孩子),我可以很好地阅读数据。但是,当我添加另一个孩子(评论)时,**显示的代码无法再读取数据库,只显示空白。餐厅类只包含吸气剂和二传手。有没有人能为我提供一个解决方案,让我可以阅读餐厅的路径'只从这段代码? (我在这里没有显示的其他代码会阅读评论路径)
Data from firebase database here
public class Info extends Fragment {
private DatabaseReference mDatabase;
private FirebaseAuth firebaseAuth;
String userID;
private TextView resNameChange, statusChange;
public Info() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_info, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference();
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
userID = user.getUid();
resNameChange = rootView.findViewById(R.id.resNameChange);
statusChange = rootView.findViewById(R.id.statusChange);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return rootView;
}
public void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
RestaurantInformation resInfo = new RestaurantInformation();
//****** CODE THAT READS THE PATH AND GET THE DATA *******
resInfo.setResName(ds.child(userID).getValue(RestaurantInformation.class).getResName());
resInfo.setStatus(ds.child(userID).getValue(RestaurantInformation.class).getStatus());
//**********************************************************
resNameChange.setText(resInfo.getResName());
statusChange.setText(resInfo.getStatus());
}
}
}
答案 0 :(得分:1)
请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference restaurantsRef = rootRef.child("restaurants");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String resName = ds.child("resName").getValue(String.class);
String status = ds.child("status").getValue(String.class);
Log.d("TAG", resName + " / " + status);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
restaurantsRef.addListenerForSingleValueEvent(eventListener);
您的输出将是:
tbfgb / gfdbfbdf
hello / there