我的RecyclerView
有问题。总之,我把一个回收器视图连接到我的数据库。然后,当我开始活动时,列表是空的。
public static class Chartviewholder extends RecyclerView.ViewHolder{
View mView;
public Chartviewholder(View itemView){
super(itemView);
mView = itemView;
}
public void setTitleProduct(String title){
TextView post_title = (TextView)mView.findViewById(R.id.tvProductName);
post_title.setText(title);
}
public void setImgProduct(String desc){
ImageView post_desc = (ImageView) mView.findViewById(R.id.productIV);
Picasso.get().load(desc).into(post_desc);
}
public void setPriceProduct( String price){
TextView post_image = (TextView) mView.findViewById(R.id.tvProductPrice);
post_image.setText(price);
}
public void setDescProduct(String desc){
TextView post_desc=(TextView) mView.findViewById(R.id.tvProductDesc);
post_desc.setText(desc);
}
}
这是我在主类中的代码,完全在方法OnCreate
dbchart = FirebaseDatabase.getInstance().getReference().child("product");
dbchart.keepSynced(true);
chartrv = (RecyclerView) findViewById(R.id.chartrv);
DatabaseReference personsRef2 = FirebaseDatabase.getInstance().getReference().child("product");
Query personsQuery2 = personsRef2.orderByKey();
chartrv.hasFixedSize();
chartrv.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions personsOptions2 = new FirebaseRecyclerOptions.Builder<Product>().setQuery(personsQuery2, Product.class).build();
chartAdapter=new FirebaseRecyclerAdapter<Product, Chartviewholder>(personsOptions2) {
@Override
protected void onBindViewHolder(@NonNull final Chartviewholder holder, int position, @NonNull final Product model) {
holder.setTitleProduct(model.product_name.toString());
holder.setImgProduct(model.product_image.toString());
holder.setDescProduct(model.Product_desc.toString());
holder.setPriceProduct(Integer.toString(model.product_price)+" DH");
Button cart=findViewById(R.id.button15);
cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addorder(model);
}
});
}
@NonNull
@Override
public Chartviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.products_list_item2, parent, false);
return new Chartviewholder(view);
}
};
chartrv.setAdapter(chartAdapter);
您需要使用startListening()
才能开始侦听数据。从文档中:
FirebaseRecyclerAdapter
使用事件侦听器来监控对 Firebase 查询的更改。若要开始侦听数据,请调用startListening()
方法。您可能希望在onStart()
方法中调用它。在调用 startListening(( 之前,请确保已完成读取数据所需的任何身份验证,否则查询将失败。
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
更多信息在这里:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-to-populate-a-recyclerview