这是我的数据库的屏幕截图:
这是活动:
private ListView dishList;
private Button bAddNewDish;
private DishListAdapter dishListAdapter;
private FirebaseDatabase mFireDb;
private DatabaseReference mDbReference;
private ChildEventListener mChildListener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dishes);
dishList = (ListView) findViewById(R.id.dishList);
bAddNewDish = (Button) findViewById(R.id.bAddNewDish);
mFireDb = FirebaseDatabase.getInstance();
mDbReference = mFireDb.getReference().child("dishlist");
ArrayList<DishElement> dishArray = new ArrayList<>();
dishListAdapter = new DishListAdapter(this,R.layout.dish_item_element,dishArray);
dishList.setAdapter(dishListAdapter);
mChildListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
DishElement dishElement = dataSnapshot.getValue(DishElement.class);
dishListAdapter.add(dishElement);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
DishElement dishElement = dataSnapshot.getValue(DishElement.class);
dishListAdapter.add(dishElement);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
DishElement dishElement = dataSnapshot.getValue(DishElement.class);
dishListAdapter.add(dishElement);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDbReference.addChildEventListener(mChildListener);
bAddNewDish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(),AddDish.class);
startActivity(i);
}
});
}
}
这是适配器:
public class DishListAdapter extends ArrayAdapter<DishElement> {
public DishListAdapter(@NonNull Context context, int resource, ArrayList<DishElement> dishElements) {
super(context, resource, dishElements);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
DishElement dishElement = getItem(position);
if(convertView == null){
convertView = LayoutInflater.from((Activity) getContext()).inflate(R.layout.dish_item_element,parent,false);
}
TextView dishName = (TextView) convertView.findViewById(R.id.dishName);
dishName.setText(dishElement.getName());
return convertView;
}
}
我使用dishElement.getName()
方法将null作为值。数据库连接似乎工作正常,但每当在我的数据库中添加子节点时,我都没有获得此字符串值。
答案 0 :(得分:0)
您在读取数据时遇到问题,因为您的字段未正确命名。要解决此问题,请使用如下所示的模型类:
public class DishElement {
private String description, image, menuId, name;
private double price;
public DishElement() {}
public DishElement(String description, String image, String menuId, String name, double price) {
this.description = description;
this.image = image;
this.menuId = menuId;
this.name = name;
this.price = price;
}
public String getDescription() {return description;}
public String getImage() {return image;}
public String getMenuId() {return menuId;}
public String getName() {return name;}
public double getPrice() {return price;}
}
要真正正确地读取数据,请从数据库中删除旧数据并添加新数据。
要阅读数据,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference dishListRef = rootRef.child("dishlist");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
DishElement dishElement = ds.getValue(DishElement.class);
dishListAdapter.add(dishElement);
Log.d("TAG", dishElement.getName());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
dishListRef.addListenerForSingleValueEvent(eventListener);
如您所见,我添加了一个日志语句,使用以下命令打印出名称:
dishElement.getName()
在这种情况下肯定会起作用。在您的情况下,由于Firebase正在寻找名为name
而非Name
(大写字母N)的字段,因此无法正常工作。