0

I have a list of apps installed on the device. Now I want to show a list of 10-20 (the number of applications should always be different) random applications, how can I do this in Kotlin?

My code:

private fun onGetApps() {
        val pm: PackageManager = requireContext().packageManager
        val apps: List<PackageInfo> = requireContext().packageManager.getInstalledPackages(PackageManager.GET_META_DATA)
        val res: ArrayList<Apps> = ArrayList<Apps>()

        for (i in apps.indices) {

            if (pm.getLaunchIntentForPackage (apps[i].packageName) != null)
            {
                val p = apps[i]

                val appInfo = Apps()

                appInfo.appName = p.applicationInfo.loadLabel(requireContext().packageManager).toString()
                appInfo.appImage = p.applicationInfo.loadIcon(requireContext().packageManager)
                res.add(appInfo)
            }

            mAdapter.setupApps(res)
            mAdapter.notifyDataSetChanged()
        }
    }

1 Answer 1

1

One way to do this is to shuffle elements randomly and take first n of them:

println((0..10).shuffled().take(3))

If the number of all items is much bigger than the number of items to take then it probably makes sense to use a sequence:

println((0..10).asSequence().shuffled().take(3).toList())

Note that in the case the number of items to get is bigger than the number of all items, it will return all items in the source collection.

In your case it will be something like:

val res = apps.asSequence()
    .filter { pm.getLaunchIntentForPackage(it.packageName) != null }
    .shuffled()
    .take((10..20).random())
    .map { p ->
        val appInfo = Apps()

        appInfo.appName = p.applicationInfo.loadLabel(requireContext().packageManager).toString()
        appInfo.appImage = p.applicationInfo.loadIcon(requireContext().packageManager)
        
        appInfo
    }.toList()
Sign up to request clarification or add additional context in comments.

6 Comments

Code inside map() can be further improved with also()/apply(), but I didn't want to add too much confusion.
The fact is that I have to select a random number of applications in Recнcler View, the number of these applications should always be different but in the range from 10 to 20. I also need to always show a different list of applications.
Yes, you said this already in the question description. What do you mean exactly with your comment? I guess one thing that is missing in my solution is that the 10 should not be fixed, but should be a random number between 10 and 20, correct? Is there anything else that is missing here?
val res = apps.asSequence() there must be an ArrayList so I can add to the adapter
What is this adapter that requires ArrayList specifically? It really doesn't make too much sense. ArrayAdapter requires array or List. ListAdapter requires List. If you created the setupApps() function and it requires ArrayList then I suggest changing it to List or MutableList. As you can see, requiring ArrayList results with compatibility problems with other code. Anyway, you can get ArrayList from my solution by changing map { ... } to: mapTo(ArrayList()) { ... } and removing toList() at the end.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.