gpt4 book ai didi

java - 空 Recyclerview firebase

转载 作者:行者123 更新时间:2023-12-02 09:14:39 25 4
gpt4 key购买 nike

我的 RecyclerView 有问题。总之,我把一个 RecyclerView 连接到我的数据库。然后,当我开始 Activity 时,列表为空。

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()开始监听数据。来自文档:

The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.

@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

关于java - 空 Recyclerview firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59093436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com