There actually is a possibility to do that without Loading the whole list client side. First you have to generate a numeric id either as child id or as an extra node.
That how your database would look like:
notes:
-KXe8LJJzczEJs3YYwRe
numericid : 001
-KXeePWrWBXvpw4g9n0p
numericid : 002
or
notes:
001
002
to create the numbers as String you can use DecimalFormat
String newint = new DecimalFormat("000").format(oldint);
Now you can get the children count in your valueeventlistener an use Math.random() to get a random child, e.g. for the second Database Design
FirebaseDatabase().getInstance().getReference().child("notes").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Long childrencount = dataSnapshot.getChildrenCount();
if(childrencount.equals(0))return;
int random = getRandomInteger(safeLongToInt(childrencount), 1);
String selectedchild = new DecimalFormat("000").format(random);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
You also need to add these safeLongtoInt and getRandomInteger
public static int getRandomInteger(int maximum, int minimum){
return ((int) (Math.random()*(maximum - minimum))) + minimum;
}
public static int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new IllegalArgumentException
(l + " cannot be cast to int without changing its value.");
}
return (int) l;
}
selectedchild is your random child id.