0

I want to display random images, Quotes (Data) from the firebase real-time database just like sql data base order by asc,desc,RAND ( ).

How can I do this firebase real time database by short line of code.

databaseReference = FirebaseDatabase.getInstance().getReference().child("Quotes").orderby ????;
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84

1 Answers1

0

Below is one way of getting data randomly its not perfect though, Reference for this code by peter(answer)

databaseReference = FirebaseDatabase.getInstance().getReference().child("Quotes");
databaseReference .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int ads = (int) dataSnapshot.getChildrenCount();
            int rand = new Random().nextInt(ads);
            for(DataSnapshot data: dataSnapshot.getChildren()){
            for(DataSnapshot datas: data.getChildren()){
            for(int i = 0; i < rand; i++) {
    
            list = new ArrayList<your-class-method-for-holding data>();
    
                Yourclass value = datas.getValue(yourclass.class);
                //Use your class to get the data and display it on your views
                //in this loop  
                
    
    }}}}
MoonLight
  • 460
  • 4
  • 15