我正在使用FirebaseRecyclerAdaptor
,但没有从数据库中获取数据。
首先,我创建一个查询:
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("items");
然后,我构建了FirebaseRecyclerOptions
对象,但是当我记录“选项快照”时,我得到的是null或空数组。
FirebaseRecyclerOptions<Item> options =
new FirebaseRecyclerOptions.Builder<Item>()
.setQuery(query, new SnapshotParser<Item>() {
@NonNull
@Override
public Item parseSnapshot(@NonNull DataSnapshot snapshot) {
return snapshot.getValue(Item.class);
}
})
.build();
Log.e("Ref", String.valueOf(options.getSnapshots()));
然后我初始化适配器:
adapter = new FirebaseRecyclerAdapter<Item, ItemsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ItemsViewHolder holder, int position, @NonNull Item model) {
holder.tv_item_price.setText(Integer.toString(model.getItemPrice()));
holder.tv_item_description.setText(model.getItemDescription());
holder.tv_item_name.setText(model.getItemName());
Picasso.with(getContext())
.load(model.getImage())
.placeholder(R.drawable.placeholder)
.into(holder.img_image);
holder.btn_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int total = Integer.parseInt(((MainActivity)getActivity()).cart_price.getText().toString()) + model.getItemPrice();
((MainActivity)getActivity()).cart_price.setText(Integer.toString(total));
selectedItems.add(model);
Log.e("00", Integer.toString(selectedItems.size()));
}
});
}
@NonNull
@Override
public ItemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_item_view, parent, false);
return new ItemsViewHolder(view);
}
};
adaptor.notifyDataSetChanged();
为什么我将快照作为空数组获取?
请注意,当我使用“ addValueEventListener”时,效果很好
// ref.addValueEventListener(new ValueEventListener() {
// @Override
// public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// items.clear();
//
// for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
// Item newItem = snapshot.getValue(Item.class);
// items.add(newItem);
// Log.i("Mysnapshot", snapshot.toString());
//
// }
//
// adaptor.notifyDataSetChanged();
// }
//
// @Override
// public void onCancelled(@NonNull DatabaseError databaseError) {
//
// }
// });
我的Firebase数据库结构如图here
我的Java类如下所示.....
public class Item implements Parcelable {
private String itemName;
private String itemDescription;
private String itemUid;
private int itemPrice;
private int itemCount;
private String image;
public Item() {} // Needed for Firebase
public Item(String itemName, String itemDescription, String itemUid, int itemPrice, int itemCount, String image) {
this.itemName = itemName;
this.itemDescription = itemDescription;
this.itemUid = itemUid;
this.itemPrice = itemPrice;
this.itemCount = itemCount;
this.image = image;
}
protected Item(Parcel in) {
itemName = in.readString();
itemDescription = in.readString();
itemUid = in.readString();
itemPrice = in.readInt();
itemCount = in.readInt();
image = in.readString();
}
public static final Creator<Item> CREATOR = new Creator<Item>() {
@Override
public Item createFromParcel(Parcel in) {
return new Item(in);
}
@Override
public Item[] newArray(int size) {
return new Item[size];
}
};
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getItemUid() {
return itemUid;
}
public void setItemUid(String itemUid) {
this.itemUid = itemUid;
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getItemCount() {
return itemCount;
}
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(itemName);
dest.writeString(itemDescription);
dest.writeString(itemUid);
dest.writeInt(itemPrice);
dest.writeInt(itemCount);
dest.writeString(image);
}
}
答案 0 :(得分:0)
您的代码中的问题是您正在使用FirebaseRecyclerAdapter
,但您没有在听更改。要解决此问题,请在onStart()
和onStop()方法中添加以下代码行,如下所示:
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if(adapter != null) {
adapter.stopListening();
}
}
这是开发人员经常犯的错误,因为在早期版本中,不需要这种做法。