0

I'm working on an application in kotlin for sending an image to a random user or to multiple user from firebase. My database is structured like this :

  • Users
    • bVk3KBL81AY2LUVUbC6G1qcLAcl2
      • username : test
      • uid : bVk3KBL81AY2LUVUbC6G1qcLAcl2

And I don't know how to get a random user from firebase, I searched but I don't find the best solution for my structure.

I try with this post : How to get unique random product in node Firebase?

But the code doesn't work I have multiple errors

Thank you in advance to any one who may be able to give me some solutions.

Edit #1 I've worked on it and it's working, but I don't know how limit the random to 3 users for example. In my application the user can change the limit of user so I need to change the random limit.

My code :

fun chooseRandomUser() {

    val rootRef = FirebaseDatabase.getInstance().getReference()
    val productsRef = rootRef.child("users")

    val valueEventListener: ValueEventListener = object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            val productList = ArrayList<String?>()
            for (ds in dataSnapshot.children) {
                val name = ds.child("username").getValue(String::class.java)
                productList.add(name)
                Log.d("FragmentActivity", "usernames : $name")
            }
            val productListSize = productList.size
            val randomProductList = ArrayList<kotlin.Int>()
            randomProductList.add(Random().nextInt(productListSize))
            Log.d("FragmentActivity", "list : $randomProductList")
        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d("FragmentActivity", "Error: ${databaseError.message}")
        }
    }
    productsRef.addListenerForSingleValueEvent(valueEventListener)

}
Chaotix
  • 21
  • 6
  • 1
    "the code doesn't work I have multiple errors" If you want our help with that, include that code in your question. Also include the exact error message you get. – Frank van Puffelen Apr 14 '20 at 21:14
  • I've worked on it and it's working but I need help for the random limit, I edit my question with the code. – Chaotix Apr 15 '20 at 16:07
  • You can limit the number of times you call `randomProductList.add(Random().nextInt(productListSize))`, with a simple loop. Just be sure to double check that you don't add the same user multiple times. Unless that is correct for your use-case of course, in which case: go for it. – Frank van Puffelen Apr 15 '20 at 16:16
  • Thank you for your help that worked, I created an EditText and create loop with that and yes I need to check for duplicate number. I'm searching how I can check that thank you. – Chaotix Apr 15 '20 at 19:33

1 Answers1

1

As far as I could understand, you want to pick a random user from all the list of users that you have in your firebase database. You can simply do that on the android side.

I think you already know how to get all the users from the firebase database. Store the users in a list. Then I would try to come up with a random function which could give me an index of the list randomly and I could pick that user from the list based on that random index.

The same implementation can go for selecting multiple users from that list as well.

I hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98