I'm working on an application, where for specific choosen items, specific .mp3 should be assigned. (Like I choose Dachshund, then I dachshund_1.mp3, dachshund_2.mp3 etc. to be played when my functions say so). Is it possible somehow, that I create a variable, that holds the specific name, then I assign it to mediaplayer.create?
What I would like to do would look like that below:
// I have a function here that returns with the specific string
// I have a cnt variable in the code which helps determine which text and sound comes now
fun DogHandler(cnt:Int, receiptName: String) :String
{
return dogName + "_" +cnt.toString()
}
This function is called, and the string it returns should go to the mediaplayer. Cnt is let's say 10
var tmp = DogHandler(dachshund, cnt); // tmp = dachsund_10
mediaPlayer = MediaPlayer.create(context, R.raw.tmp)
mediaPlayer.start()
To my knowledge there is no way to generate resource IDs (like R.raw.something) dynamically. However, you could simply create a list of them and access it by index.
val dachsunds = listOf(
R.raw.dachsund_0,
R.raw.dachsund_1,
R.raw.dachsund_2,
R.raw.dachsund_3,
// ...
R.raw.dachsund_10
)
val dachsund = dachsunds[dachsundIndex]
val mediaPlayer = MediaPlayer.create(context, dachsund).apply {
start()
}
Related
I'm trying to list the first 100 of a shuffled list. I'm telling it to shuffle if the list is at 0 and then increment. I then am trying to call that list in another section of the when but it's not working. How can I accomplish this?
when (countF) {
0 -> {
//shuffle at 0
val randomChaos = chaosList.asSequence().shuffled().take(chaosList.count()).toList()
cResult.text = randomChaos.elementAt(countF) + countF + "\n\n\n\n\n\n\n\n" + this.cResult.text
countF++
}
1-99 -> {
//show 1-99
cResult.text = randomChaos.elementAt(countF) + countF + "\n\n\n\n\n\n\n\n" + this.cResult.text
countF++
}
100 -> countF = 0
You would need to create the val randomChaos before the when enclosure for it to be available in the scope of multiple branches of the when statement.
That said, the way you're getting a random element is very convoluted. take(chaosList.count()) is completely redundant. And since you don't use multiple sequence operators, creating a sequence is also redundant. Finally, you are only pulling a single item from the random list, so it's unnecessary to create a shuffled list in the first place. Using elementAt() on a shuffled list is no different than picking any element out of that shuffled list, or simply picking a random item out of a list that isn't shuffled at all.
Also, the first two branches of your when statement currently would produce exactly the same results so they can be merged.
Based on what you described, I'm guessing you had this when statement inside a loop that tries to run it 100 times so you can list all the items. For that to work, you would need to shuffle the list one time outside the loop, and then you could iterate its elements in the loop.
However, there are functions that can make it easier to do what you're suggesting. Here's an example:
val randomChaos = chaosList.shuffled()
cResult.text = randomChaos.asSequence()
.take(100)
.withIndex()
.joinToString("\n") { (i, value) ->
"$value-$i"
}
In this case, using a Sequence helps avoid creating an intermediate list to hold the first 100 values.
var randomChaos = chaosList.shuffled()
fun cShuf() { randomChaos = chaosList.shuffled() }
cRoll.setOnClickListener() {
cResult.movementMethod = ScrollingMovementMethod()
if (countF < 1) { cShuf() }
cResult.text = randomChaos.elementAt(countF) + "\n\n\n\n\n\n\n\n" + this.cResult.text
countF++
if (countF > 100) countF = 0
}
I have figured out how to use a function to generate a new shuffe of the list once I've hit > 100 shown.
My issue with making it a function was I was trying to use val variable in the function but the variable already existed so I didn't need to use val, just the name of the variable.
I have an app in which gives you a certain photo based on an integer being fetched via
class RandomImageLogic(){
fun retrive(): Int{
return (1..9).random()
}
}
However, it is not professional to have a repeated outcome, as I desire a random integer to be fetched each time I call the function in order for the image to be different each time my button is pressed. How can I fetch a new random integer whenever the button calls the function?
The easy way is to pass in the last random number you received and filter it out.
fun retrive(except: Int): Int{
return ((1..9).filter {it != except}).random();
}
In your case, this method might not be called extremely often (only when the user clicks a button).
If this method was to be called more often, filter on an IntRange should be used with care (as proposed in #avalerio's answer).
This iterates the whole range (unnecessarily costing time) and it would create a temporary ArrayList on every call (creating unnecessary garbage and triggering the garbage collector more often than needed).
Here is a sample object NonRepeatingRandom (you can also implement it as a class if you wish). retrieve (which has been expanded with a max parameter and basic sanity checks) recursivly calls itself again if the same number would be generated twice in a row:
object NonRepeatingRandom {
private var previous = -1
fun retrieve(max : Int = 9): Int {
if(max < 0) {
error("Only positive numbers")
}
if(max <= 1) {
// There is nothing random about 0 or 1, do not check against previous, just return
previous = max
return max
}
val rand = (1..9).random()
return if(rand == previous) {
retrieve(max) // recursive call if two subsequent retrieve() calls would return the same number
} else {
previous = rand // remember last random number
rand
}
}
}
fun main(args: Array<String>) {
repeat(1000) {
println(NonRepeatingRandom.retrieve())
}
}
I made a quick-and-dirty performance test, calling my "recursive" method 10 million times and calling the "filter" method 10 million times.
Recursive: 125 ms (10 mio calls)
Filter: 864 ms (10 mio calls)
Pre-fill & random shuffle approach:
class RandomIntIterator(
private val range: IntRange
) : Iterator<Int> {
private lateinit var iterator: Iterator<Int>
init { randomize() }
fun randomize() {
iterator = range.shuffled().iterator()
}
override fun hasNext() = iterator.hasNext()
override fun next() = iterator.next()
}
...
val rnd = RandomIntIterator(1..9)
...
// on button click
if (rnd.hasNext()) {
val num = rnd.next()
// use num
} else {
// renew (if needed)
rnd.randomize()
}
I like using Sequences to generate unending streams of values.
In this case we'll have to write custom code, as checking for repeated values is a stateful operation, and while Sequences has a distinct() stateful filter, it applies to all generated values - we only want it to apply to a limited window.
TL;DR:
class RandomImageLogic(
private val random: Random,
/** The number of sequential values that must be distinct */
noRepeatsLimit: Int = 2
) {
private val sourceValues: List<Int> = (0..9).toList()
private fun nextValue(vararg exclusions: Int): Int =
(sourceValues - exclusions.asList()).random(random)
private val randomInts: Iterator<Int> =
generateSequence({
// the initial value just has one random int
val next = nextValue()
ArrayDeque(listOf(next))
}) { previousValues ->
// generate the next value, excluding previous values
val nextValue = nextValue(*previousValues.toIntArray())
// limit the size of previousValues, if necessary
if (previousValues.size >= noRepeatsLimit)
previousValues.removeLastOrNull()
// add the generated value to the beginning of the deque
previousValues.addFirst(nextValue)
previousValues
}
.map {
// convert the Sequence to a list of ints,
// each element is the first item in the deque
it.first()
}
.iterator()
fun retrieve(): Int {
return randomInts.next()
}
}
Testing
Let's write a test first, to make sure our solution works. Kotest has a specific property based testing subproject, and this will let us cover a wide range of test cases very quickly.
So, I ran through the setup, and started setting up the test case.
Seeding RandomImageLogic
First I modified the RandomImageLogic class so that the random selection could be seeded with a provided Random.
import kotlin.random.Random
class RandomImageLogic(private val random: Random) {
fun retrieve(): Int {
return (1..9).random(random = random)
}
}
This will help us to create a Generator for the RandomImageLogic.
Testing all values
Now we can use Kotest to write a property-based test that will assert "for all sequential values, they are different"
import io.kotest.core.spec.style.FunSpec
import io.kotest.property.arbitrary.arbitrary
import io.kotest.property.forAll
class RandomImageLogicTest: FunSpec({
// This generator will create a new instance of `RandomImageLogic`
// and generate two sequential values.
val sequentialValuesArb = arbitrary { rs ->
val randomImageLogic = RandomImageLogic(rs.random)
val firstValue = randomImageLogic.retrieve()
val secondValue = randomImageLogic.retrieve()
firstValue to secondValue
}
test("expect sequential values are different") {
forAll(sequentialValuesArb) { (firstValue, secondValue) ->
firstValue != secondValue
}
}
})
Of course, the test fails.
Property failed after 4 attempts
Arg 0: (1, 1)
Repeat this test by using seed 1210584330919845105
Caused by org.opentest4j.AssertionFailedError: expected:<true> but was:<false>
So let's fix it!
Generating Sequences
As I said earlier, I really like Sequences. They're perfect for this use-case, where we have an infinite source of values.
To demonstrate how to make a Sequence, let's convert the existing code, and use an Iterator to fetch values.
class RandomImageLogic(private val random: Random) {
private val randomInts =
// generate a sequence using values from this lambda
generateSequence { (1..9).random(random = random) }
// use an iterator to fetch values
.iterator()
fun retrieve(): Int {
return randomInts.next()
}
}
This hasn't solved the problem yet - the Sequence only generates and provides one value at a time and so cannot do any filtering. Fortunately generateSequence() has an variant with nextFunction: (T) -> T?, where we can determine the next value based on the previous value.
If we use this constructor, and do a bit of refactoring to share the source values, and a util method to generate a next value while filtering out previous values...
private val sourceValues: List<Int> = (0..9).toList()
private fun nextValue(vararg exclusions: Int): Int =
(sourceValues - exclusions.asList()).random(random)
private val randomInts: Iterator<Int> =
generateSequence({ nextValue() }) { previousValue ->
nextValue(previousValue)
}
.iterator()
and now if we run the test, it passes!
Test Duration Result
expect sequential values are different 0.077s passed
Improvement: more than two distinct sequential values
What happens if you don't just want two sequential values to be distinct, but 3? Or even more? Let's make the 'no-repeated-values' limit configurable, and I think this will demonstrate why Sequences are a good solution.
class RandomImageLogic(
private val random: Random,
/** The number of sequential values that must be distinct */
noRepeatsLimit: Int = 2
) {
// ...
}
Testing
Once again, let's write a test to make sure things work as expected.
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.collections.shouldNotContainDuplicates
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.checkAll
import kotlin.random.Random
class RandomImageLogicTest : FunSpec({
test("expect arbitrary sequential values are different") {
checkAll(Arb.int(), Arb.int(1..10)) { seed, noRepeatsLimit->
val randomImageLogic = RandomImageLogic(Random(seed), noRepeatsLimit)
val result = List(noRepeatsLimit) { randomImageLogic.retrieve() }
withClue("Result: $result") {
result shouldHaveSize noRepeatsLimit
result.shouldNotContainDuplicates()
}
}
}
})
And of course the test fails.
Property test failed for inputs
0) -459964888
1) 5
Caused by java.lang.AssertionError: Result: [3, 8, 0, 2, 8]
Collection should not contain duplicates
There's lots of options to make a Sequence stateful - again let's just look at one.
Sequence of values
Instead of a sequence of individual values, we can have a sequence where each element is a list of not only the current value, but also previously seen values.
Let's use ArrayDeque to store these values, because it's easy to add and remove values from the start and end.
Again, we use the same generateSequence constructor with a seedFunction and nextFunction - except this time each element is deque which stores all values, and in nextFunction we add new values to the start of the deque, trimming it if it's larger than the window size noRepeatsLimit
private val randomInts: Iterator<Int> =
generateSequence({
// the initial value just has one random int
val next = nextValue()
ArrayDeque(listOf(next))
}) { previousValues ->
// generate the next value, excluding previous values
val nextValue = nextValue(*previousValues.toIntArray())
// limit the size of previousValues, if necessary
if (previousValues.size >= noRepeatsLimit)
previousValues.removeLastOrNull()
// add the generated value to the beginning of the deque
previousValues.addFirst(nextValue)
previousValues
}
.map {
// convert the Sequence to a list of ints,
// each element is the first item in the deque
it.first()
}
.iterator()
And yup, the test passes!
Test Duration Result
expect arbitrary sequential values are different 0.210s passed
Storing state
It's important to think about how state will be stored. 'State' is required by RandomImageLogic to know whether a generated value is distinct.
In the Sequence implementation, it's stored internally, and so is specifically associated with an instance of RandomImageLogic. Maybe your application only has one instance of RandomImageLogic at any one time, in which case the state will always be up to date and will be shared between all invocations.
But what happens if there's more than one instance of RandomImageLogic? Or if there's multi-threading? Or if the RandomImageLogic instance is recreated?
The answers to these depend on the implementation and situation. From your question I suspect that it's not critically important that images never repeat, but I bring this up because it is important to think about.
How can I replace something like:
when (content[position].ImageSrc) {
1 -> holder.imageView.setImageResource(R.drawable.image_1)
2 -> holder.imageView.setImageResource(R.drawable.image_2)
3 -> holder.imageView.setImageResource(R.drawable.image_3)
else -> {
holder.imageView.setImageResource(R.drawable.image_x)
}
}
more elegant so something like:
var a = "image_"
var b = content[position].ImageSrc
var c = a + b
holder.imageView.setImageResource(R.drawable.c)
The first code is working but way to laborious, the second one isn't.
Thx:)
Make a Repository that can resolve the asset for you...
in your UI you'd do:
holder.imageView.setImageResource(
myResourceRepo.getImageResourceFor(content[position].ImageSrc)
)
Now what you do in there, it really depends, if you really have 100 resources, you have to find a way to MAP them together, either by creating a mapOf(Int, "img source") (whatever that is), where Int is the R.drawable.xxx and img source is whatever is in content[...].imageSrc (a String?)
This is how you map/associate them together.
If you want it to be more dynamic, then store your images in "raw assets" and using the AssetManager, load the image and convert it into a Drawable at runtime. This way you get to "construct" the asset name like you did
var filename = "some/path/" + content[...].imageSrc
val file = assetManager.open(filename)...
val drawable = makeDrawableFrom(file)
return drawable
Your viewHolder will then use it as
val drawable = repo.getDrawableFrom(content[x].imageSrc)
holder.imageView.setImageResource(drawable)
(NOTE: most of these are "pseudo-code", you have to implement most of it, but it's all possible and quite simple).
If you REALLY want to try to load a resource dynamically... it used to be possible with something like
val nameOfResource = "a" + "b" + "c" //construct your "dynamic" name
val drawable = context.resources.getIdentifier(nameOfResource,
"drawable", context.getPackageName())
(not sure if this still works)
I doubt your "second method" exist instead, you can reformat your first one as follows,
holder.imageView.setImageResource(
when (content[position].ImageSrc) {
1 -> R.drawable.image_1
2 -> R.drawable.image_2
3 -> R.drawable.image_3
else ->R.drawable.image_else
}
)
val imageId = resources.getIdentifier("com.packagename.app:drawable/image_${content[position].ImageSrc}", null, null);
holder.imageView.setImageResource(imageId)
please help. I need to add 2 sounds ogg to one action, which will change randomly. How do I do that? I don't know how to do it. Thank you very much.
Thread(Runnable {
val assets = context.resources.assets
sounds[SOUND_DIE] = soundPool.load(assets.openFd("die.ogg"), 1)
sounds[SOUND_HIT] = soundPool.load(assets.openFd("hit.ogg"), 1)
sounds[SOUND_POINT] = soundPool.load(assets.openFd("point.ogg"), 1)
sounds[SOUND_SWOOSHING] = soundPool.load(assets.openFd("swooshing.ogg"), 1)
sounds[SOUND_WING] = soundPool.load(assets.openFd("wing.ogg"), 1)
}).start()
}
You can call .random() on a collection to get a random item. So instead of mapping each sound type (like SOUND_HIT) to one sound, map each one to a list of sounds (which can contain one item if that's all you have). And then for every sound you load, just add it to the appropriate list.
That way when you want to play a sound, you can go play(sounds[SOUND_SWOOSHING].random()) and it will just pick one from that sound type's list.
You can set it up the way you're doing now
sounds = mapOf(
SOUND_DIE to listOf(
soundPool.load(assets.openFd("die.ogg"), 1),
soundPool.load(assets.openFd("yargh.ogg"), 1)
),
SOUND_HIT to ...
)
but I'd recommend adding a function to handle all that loading:
fun loadSound(filename: String) = soundPool.load(assets.openFd(filename), 1)
sounds = mapOf(
SOUND_DIE to listOf(
loadSound("die.ogg"),
loadSound("yargh.ogg")
),
SOUND_HIT to ...
)
or if you want to get fancy...
val filenamesToTypes = mapOf(
"die.ogg" to SOUND_DIE,
"yargh.ogg" to SOUND_DIE,
"point.ogg" to SOUND_POINT,
...
)
// build your sounds collection by grouping all the filenames
// with the same sound type, and transform each filename to a
// loaded sound, so you get a map of SoundType -> List<Sound>
sounds = filenamesToTypes.entries.groupBy(
keySelector = { it.value },
valueTransform = { loadSound(it.key) }
)
Don't worry if that feels too complicated, the first couple of examples are neat enough and hopefully easy to follow! I just like when you can organise stuff all snappy :)
I have a user interface with multiple buttons. They have the IDs "button1", "button2", ...
I want to set an OnClickListener for all of them in a for loop. I dont want to type a line like button1.setOnClickListener for every button.
I have found one solution that works in java here: Android: Using findViewById() with a string / in a loop
And I tried to adapt it in Kotlin.
var buttons = ArrayList<Button>()
for (i in 1..7) {
var idString = "Button%i"
var buttonID = getResources().getIdentifier(idString, "id", packageName)
buttons.add( findViewWithTag(buttonID))
buttons[i].setOnClickListener(buttonclicked)
}
This throws an "Unresolved Reference" error. How can I access all buttons without typing a line for each of them?
Thanks in advance to all of you.
You call findViewWithTag() instead of findViewById() in your code.
Also you are not doing string interpolation correctly by var idString = "Button%i".
Change to this:
val buttons = ArrayList<Button>()
for (i in 1..7) {
val idString = "Button$i"
val buttonID = resources.getIdentifier(idString, "id", packageName)
buttons.add(findViewById(buttonID))
buttons[i].setOnClickListener(buttonclicked)
}