0

I have a problem with my RecyclerView. In summary, I put a RecyclerView connected to my database. Then when I start the activity the list is empty.

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);
    }



}

this is my code in the mainclass exactly in the methode 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);
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
omaros
  • 33
  • 5

1 Answers1

1

You need to use startListening() to begin listening for data. From the docs:

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();
}

More information here:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-to-populate-a-recyclerview

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134