我尝试了几种方法,但仍然无法检索其中的数据。我可能为我的QuotationOrder类创建了错误的模型,因为它的订单数量为0,1,2...。我是Firebase的新手。不胜感激。
这是代码和我的模型。请指导我是否为它创建了正确的模型。
List<QuotationOrder> quoteList;
quoteList = new ArrayList<>();
final DatabaseReference quotationListRef = FirebaseDatabase.getInstance().getReference()
.child("QuotationOrder")
.child(Prevalent.currentOnlineUser.getPhone());
quotationListRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) { Log.e("Query Refa",dataSnapshot.getRef().toString());
Log.e("Query DataSa","\n "+dSnapshot.toString() );
for(DataSnapshot ds : dSnapshot.getChildren()) {
QuotationOrder quote = ds.getValue(QuotationOrder.class);
quoteList.add(quote);
Log.e("Query Refa",dataSnapshot.getRef().toString());
Log.e("Query Refb",ds.getRef().toString());
Log.e("Query DataSb","\n "+ds.toString() );;
public class QuotationOrder {
String itemID;
String itemName;
String itemPrice;
String itemDescription;
String itemCategoryType;
String itemQuantity;
ArrayList<Cart> order;
public QuotationOrder() {
}
public QuotationOrder(String itemID, String itemName, String itemPrice, String itemDescription, String itemCategoryType, String itemQuantity, ArrayList<Cart> order) {
this.itemID = itemID;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemDescription = itemDescription;
this.itemCategoryType = itemCategoryType;
this.itemQuantity = itemQuantity;
this.order = order;
}
public ArrayList<Cart> getOrder() {
return order;
}
public void printOrders() {
for (Cart carts : this.order) {
System.out.println(carts);
}
}
更新 在遵循您的建议之后,arraylist quotelist的大小仍然为0。此外,我还添加了我的模型类。请指导我是否为它创建了正确的模型。谢谢。 Database Structure
UPDATE2 似乎两个循环的数据快照具有下图的值。我面临的问题是存储它。如果我没有记错,似乎数据无法存储在arraylist中?如何储存?我的模型出错了吗? 来自Log.d(“ TAG”,String.valueOf(quote.getItemCategoryType()))的错误;可能会产生空指针异常
答案 0 :(得分:0)
因此,在亚历克斯(Alex)的一些帮助下以及其他帖子的一些技巧下,我设法找出了问题的答案。当实际上是字符串的itemPrice声明为int时,在我的模型类中实际上出了点问题。不过,谢谢您的帮助,Alex。
List<Cart> itemList;
itemList = new ArrayList<>();
final DatabaseReference quotationListRef = FirebaseDatabase.getInstance().getReference()
.child("QuotationOrder")
.child(Prevalent.currentOnlineUser.getPhone());
quotationListRef.child(Prevalent.currentOnlineUser.getPhone())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for (DataSnapshot quotationSnapshot: dataSnapshot.getChildren())
{
for (DataSnapshot productSnapshot: quotationSnapshot.getChildren())
{
Cart cart = productSnapshot.getValue(Cart.class);
itemList.add(cart);
for(Cart a: itemList)
{
Log.e("cart","\n "+a.getItemCategoryType() );
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
throw databaseError.toException();
}