I am trying to get string value from string file like this:
var language = arrayListOf<String>(
R.string.All_Categories.toString(),
)
but it shows an Int rather than a string like this:
What am I doing wrong?
R.string.All_Categories is the id, not the string itself
To get the string you need to use
var value = getString(R.string.All_Categories)
The issue is you are doing toString on the generated reference. You should instead use R.array.All_Categories if that is the name of your referenced array. For example, you have the following in the resources file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="my_books">
<item>Scala Cookbook</item>
<item>Play Framework Recipes</item>
<item>How I Sold My Business: A Personal Diary</item>
<item>A Survival Guide for New Consultants</item>
</string-array>
</resources>
This is how you would want to read it in the code.
Resources res = getResources();
String[] myBooks = res.getStringArray(R.array.my_books);
Kotlin code example in fragment:
val res: Resources = resources
val myBooks: Array<String> = res.getStringArray(R.array.my_books)
You can use .toList() on the Array<String> to convert it to a collections object like the arrayListOf<String> expected by you.
SOLUTION:
first, in OnCreate must define the string:
val Profile_Settings = resources.getString(R.string.ProfileSettings)
second, add the value to the array:
var language2 = arrayListOf<String>(
Profile_Settings,
)
it's very simple, you just have to use getString(your_string_id) and you are good to go!
which looks like this:
val data = getString(R.string.all_categories)
Related
I have a long string array list of "Animals" that I need to associate a code number with.
Once the "Animal" is selected via my spinner the value is stored in a variable. I also want to have the associated code number stored in its own variable.
How do I go about creating this "pairing" without writting a ton of if/then code. Can I do anything within my strings.xml file that contains my string-array?
<string-array name="Animals">
<item>Dog</item>
<item>Cat</item>
<item>Mouse</item>
...
"Dog" paired with code: "111"
"Cat" paired with code: '222"
"Mouse" paired with code:"333"
You can create the corresponding integer-array and zip them together. There is one BIG WARNING with this though, you have to make sure that if you change one of the arrays, you must update the other too!
Kotlin playground example:
fun main() {
val stringArray: Array<String> = arrayOf("Dog", "Cat")
val intArray: Array<String> = arrayOf("1", "0")
print(sArray.zip(iArray))
}
If the corresponding code is going to be only their index in the array it's simple as:
arrayOf("Dog", "Cat").mapIndexed { index, animal -> index to animal }
So with your example it would be something like this:
<string-array name="Animals">
<item>Dog</item>
<item>Cat</item>
<item>Mouse</item>
...
</string-array>
<integer-array name="AnimalsNumberCodes">
<item>111</item>
<item>222</item>
<item>333</item>
...
</integer-array>
val listOfPairs = resources.getStringArray(R.array.Animals)
.zip(
resources.getIntArray(R.array.AnimalsNumberCodes).toTypedArray()
)
To address the change in the question. All you have to do to get that lookup is to change to a map.
spinnerMap = resources.getStringArray(R.array.Animals)
.zip(
resources.getIntArray(R.array.AnimalsNumberCodes).toTypedArray()
).toMap()
spinnerMap["Dog"] // "111" or whatever you zip it with
My output looks like this :
["Floor 0","Floor 1","Floor 2"]
It comes as a string. But I want to fetch each element of this array. How can I do this using Kotlin ?
implement this library Gson
you can use it like this
val text = "[\"Floor 0\",\"Floor 1\",\"Floor 2\"]"
val array = Gson().fromJson(text, ArrayList::class.java)
array.forEach {
Log.e(TAG, "onCreate: it $it")
}
Just use regular expressions to create a match for each CharSequence between the double quotes. As you want to use only the values between the quotes, you can extract the first index group values. The following code snippet does what you are asking for in Kotlin:
val str = "[\"Floor 0\",\"Floor 1\",\"Floor 2\"]"
val pattern = Regex( "\"(.*?)\"")
val fetched_elements = pattern.findAll(str).map {
it.groupValues[1]
}.toList()
// creates the list: [Floor 0, Floor 1, Floor 2]
Use also this RegExr example to explore this in detail with explanation.
If your internal strings aren't allowed to have commas, you could do it with a split function to convert it into a list:
var lst = str.replace("\"", "").split(",")
If your internal strings can have trailing whitespace, this would be better:
var lst = str.replace("\"", "").split(",").map { it.trim() }
In the above code lines, the replace function removes the quotes surrounding each internal string; the split separates the string at each comma; and the trim function removes any surrounding whitespace characters.
If your internal strings can contain commas, you're better off learning about and using regular expressions as mentioned in another answer.
I don't know why array is not recognized when I create a string and type the following code to access it in MainActivity.kt:
var values: Array <String> = resources.getStringArray (R.array.names)
Define the data type of array just like below:
var values = resources.getStringArray (R.array.system)
Or
just write var values = resources.getStringArray (R.array.names)
It will automatically get the required type.
Define the array like below:-
<string-array name="system">
<item>p</item>
<item>fdd</item>
</string-array>
I have strings in my strings.xml e.g.:
<string name="category__service">Service</string>
I want to access them like this:
val key = "category__$this.name" // "category__service"
val s = R.string.[key]
This would give me the Id of the string which I can use.
But this way I get the error
The expression cannot be a selector (occur after a doted text)
I also tried
val s = R.string.$key
but I get:
Expecting an element
The documentation on what R is to begin with, isn't giving me much. As far as I see – R.string does not have a simple getter.
So at this point I'm just guessing for a solution. Is this even possible in Kotlin?
You can try following:
val key = "category__$this.name" // "category__service"
val s = resources.getIdentifier(key, "string", context.packageName)
Begin new project in Kotlin and missing those.
Try to get string-array recources but can't.
In strings.xml I palced next items.
<string-array name="themeList">
<item>white</item>
<item>sepia</item>
<item>black</item>
<item>pink</item>
</string-array>
In code I try next:
val res: Resources = resources
val appThemeList = arrayOf(res.getStringArray(R.array.themeList))
for (value in appThemeList) {
Log.i ("value", value.toString())
}
But in logCat i see:
I/value: [Ljava.lang.String;#40145f2
And I don'r understand, what I do incorrectly.
replace
val appThemeList = arrayOf(res.getStringArray(R.array.themeList))
to
val appThemeList = res.getStringArray(R.array.themeList)
In other case you got array
val myArray = res.getStringArray(R.array.themeList) //already array
And added to another array
arrayOf(myArray) // array of arrays
In android is depend on context when outside Activity like this
val themes = context.resources.getStringArray(R.array.themeList)
or without context is direct to resource when inside Activity
val themes = resources.getStringArray(R.array.themeList)
As we know res.getStringArray return arraylist so you do not need to write arrayOf on your code.
Simple way to achieve your goal is:-
val list = res.getStringArray(R.array.list);
We can use arrayOf when we have to define our array or we have already arraylist like below :-
val myArray = arrayOf(4, 5, 7, 3);
Try this,
val Lines = Arrays.asList(resources.getStringArray(R.array.list))
In kotlin use :
var yourVar = resources.getStringArray(R.array.your_string_array)
In kotlin, also need to use context like below
var arry = context.resources.getStringArray(R.array.your_string_array)
With this line of code you get exactly the element in the place of index.
val the_string = resources.getStringArray(R.array.YOUR_STRING_ARRAY)[index].toString()
This will be your res/values/strings.xml
<string-array name="YOUR_STRING_ARRAY">
<item>kJ</item>
<item>kWh</item>
<item>Btu</item>
<item>kcal</item>
</string-array>
As an example if the index is 1 then the return value is "kWh"
If you want use the path of resources in RecyclerView.Adapter Class you must put into function onBindViewHolder
val myItem = holder.itemView.resources.getStringArray(R.array.myItem_string_array)