Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Display random string from Array stored in values/ with Kotlin?

Posted on November 11, 2022 By

Solution 1 :

You should use getStringArray instead of arrayOf. It is method of collections. Try this

val rarity = resources.getStringArray(R.array.wp_rarity)

Solution 2 :

The answer was given by Cong Hai in the comments of his post.

It was :

val rarity = resources.getStringArray(R.array.wp_rarity)

Many thanks to him !

Problem :

I searched for a way to display in my TextView a random string from my array list stored in Values/Rarity.xml.

There are many examples here all in Java, but none are working.

I tried:
kotlin Get random string from array (can’t make it work with an array from my values/rarity.xml)

Getting random values from an ArrayList

I am new to Android studio and would like to know how to make it work, has I’ve been trying for the past few hours without success.

This is what I would like to achieve, but the app crash when I click on the button :

package com.example.myapplication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val rarity = arrayOf(R.array.wp_rarity)
        val randomRarity = rarity.random()

        btn_generate.setOnClickListener {
            item_type.setText(randomRarity)
        }
    }
}

If I replace the val rarity = arrayOf(R.array.wp_rarity) by val rarity = arrayOf("test", "test2") it does work but I would like to use my arrays in values/*.xml in order to optimize it. Or is there another way ?

Regards,

Comments

Comment posted by Daniel Morena

I used getStringArray however it’s highlighted in RED and doesn’t seem to work.

Comment posted by Công Hải

Try to getResouce().getStringArray instead

Comment posted by Daniel Morena

Thanks, that was the answer ! To be more specific it was :

READ  [FIXED] android - Firebase issue, multiple errors after upgrade
Powered by Inline Related Posts
Android Tags:android, arrays, kotlin, random, string

Post navigation

Previous Post: [FIXED] java – How can I solve this Android Studio Firebase problem?
Next Post: [FIXED] Android Java Publish result cast issue at MyCustomAdapter.java

Related Posts

[FIXED] java – My app force close when call putString on SharedPreference.Editor Android
[FIXED] android – genymotion Could not find a player plugin Android
[FIXED] java – Why i failed to fetch body from Api using retrofit in android. and it’s return code 500 and it’s return all my data in error body Android
[FIXED] arrays – How to convert a jsonobject into a jsonarray and also change all keys with all custom same keys in Java and Android Android
[FIXED] java – facing difficulty to download gradle in dolphin android version Android
[FIXED] java – Why do setContentView(R.layout.activity_main) and findViewById(R.id.example) not use dot notation to reference an object? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme