1
    recyclerView = findViewById(R.id.recyclerView);

    database = FirebaseDatabase.getInstance();
    reference_root = database.getReference("/image");
    reference_grocery_and_staples = database.getReference("image/Grocery & Staples");
    reference_beverages = database.getReference("image/Beverages");
    reference_home_and_kitchen = database.getReference("image/Home & Kitchen");
    reference_furnishing_and_home_needs = database.getReference("image/Furnishing & Home Needs");
    reference_household_needs = database.getReference("image/Household Needs");
    reference_personal_care = database.getReference("image/Personal Care");
    reference_breakfast_and_dairy = database.getReference("image/Breakfast & Dairy");
    reference_biscuits_snacks_and_chocolates = database.getReference("image/Biscuits, Snacks & Chocolates");
    reference_noodles_sauces_and_instant_food = database.getReference("image/Noodles, Sauces & Instant Food");
    reference_baby_and_kids = database.getReference("image/Baby & Kids");
    reference_pet_care = database.getReference("image/Pet Care");
    reference_frozen_food = database.getReference("image/Frozen Food");
    reference_vegetables = database.getReference("image/Vegetables");




    layoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(layoutManager);


    final ArrayList<DatabaseReference> reference = new ArrayList<>();
    reference.add(reference_grocery_and_staples);
    reference.add(reference_beverages);
    reference.add(reference_home_and_kitchen);
    reference.add(reference_furnishing_and_home_needs);
    reference.add(reference_household_needs);
    reference.add(reference_personal_care);
    reference.add(reference_breakfast_and_dairy);
    reference.add(reference_biscuits_snacks_and_chocolates);
    reference.add(reference_noodles_sauces_and_instant_food);
    reference.add(reference_baby_and_kids);
    reference.add(reference_pet_care);
    reference.add(reference_frozen_food);
    reference.add(reference_vegetables);




    for(int i = 0; i < 13; i++) {
        FirebaseRecyclerOptions<ImageModel> options = new FirebaseRecyclerOptions
                .Builder<ImageModel>()
                .setQuery(reference.get(i), ImageModel.class)
                .build();


        adapter = new FirebaseRecyclerAdapter<ImageModel, HomePage.ImageHolder>(options) {

            @NonNull
            @Override
            public HomePage.ImageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater
                        .from(HomePage.this)
                        .inflate(R.layout.row_layout, parent, false);
                HomePage.ImageHolder holder = new HomePage.ImageHolder(view);
                return holder;
            }

            @Override
            protected void onBindViewHolder(@NonNull HomePage.ImageHolder holder, int position, @NonNull final e.shweta.authenticationdemo.ImageModel model) {

                String url = model.getProductURL();
                String name = model.getProductName();
                String price = model.getProductPrice();
                String descritpion = model.getProductDescription();
                holder.textView1.setText(name);
                holder.textView2.setText("Price: Rs. " + price);
                Picasso.get()
                        .load(url)
                        .into(holder.imageView);
                holder.linearLayoutRowLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(HomePage.this, e.shweta.authenticationdemo.ProductInfo.class);
                        intent.putExtra("productImage", model.getProductURL());
                        intent.putExtra("productName", model.getProductName());
                        intent.putExtra("productPrice", model.getProductPrice());
                        intent.putExtra("productDescription", model.getProductDescription());
                        startActivity(intent);

                    }
                });

            }

            @Override
            public void onDataChanged() {
                super.onDataChanged();
                adapter.notifyDataSetChanged();
            }
        };
    }

    recyclerView.setAdapter(adapter);

I want to show the data of all the children in my home page. I have tried to loop the references to the children. But what it is doing is putting the data of only the final loop to the page. I guess it is doing it because it is updating the view every time.

It is only showing me the data of the final loop, i.e the vegetables i want the data on my homepage of every child.

database screenshot

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You cannot achieve this using this database structure because all the children are not of the same type. You have custom nodes along with pushed ids. Change the database structure to have children of the same type and **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Aug 24 '18 at 11:11
  • Then how will i be able to access the categorised data. For example: what will be the query in the vegetables page so as it only receives the data for the vegetables. – shivam chawla Aug 24 '18 at 13:04
  • So you only want to display the children under vegetables, which in your picture is only one? – Alex Mamo Aug 24 '18 at 13:07
  • I will add more to it sir, But I do have a whole dedicated page for every category. I want to retrieve the data of every category on its page also. And I also want to have all the the data of all the categories on my homepage. – shivam chawla Aug 24 '18 at 13:15
  • I am thinking of something as making an category attribute in every child. And then putting something like: Query query = reference.orderByValue().equalTo("Vegetables"); – shivam chawla Aug 24 '18 at 13:22

1 Answers1

2

You need to structure your data differently. Add another field like productType which can be beverages, biscuits etc. If you are ready to do this then just create a class Product.java:

    public class Product {

    private String productName, productPrice;//etc

    //add setters and getters
}

To get data and update recycler view

    private List<Product> productList;
productList = new ArrayList<>();
mRef = database.getReference().child(); //path
        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Iterable<DataSnapshot> productData = dataSnapshot.getChildren();

                for(DataSnapshot d : productData){

                    Product product = d.getValue(Product.class);

                    if(product.getProductType() == "Beverages"){  //getProductType() is the getter from Product.java
                    productList.add(product);
                    recyclerAdapter.notifyDataSetChanged();
                }   

                //Now only beverages are added to recycler view
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Pass the List item to the adapter

recyclerAdapter = new mRecyclerAdapter(getContext(), productList);

modify your adapter constructor to accept these values

sammy
  • 296
  • 2
  • 6