0

I want to get "productTitle" of all products randomly. but currently, this code only gets "productTitle" of the last added product. That is my problem.

How can I get the productTitle of all products randomly?

Once I have solved the above, my final goal is to get a random image that corresponds to a product with a productCategory of "bottom". Help me Please.

  String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    DatabaseReference randomsRef = db.child("Users").child(uid).child("Products");

    randomsRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            int count = (int) dataSnapshot.getChildrenCount();
            for(DataSnapshot datas: dataSnapshot.getChildren()){
                int rand = new Random().nextInt(count);
                for(int i = 0; i< rand; i++){
                    String productTitle = datas.child("productTitle").getValue().toString();
                    testRandomText.setText(productTitle);
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
kmskms
  • 11
  • 2

0 Answers0