String-array in Kotlin - android

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)

Related

How to Pair Item in String Array?

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

How to make an array of arrays in kotlin?

I have an array of userIDs, which contains specific users from a certain group.
{userID1,userID2}
val userIDArrayList: ArrayList<String> = ArrayList()
userIDArrayList.add(userID1)
userIDArrayList.add(userID2)
I want to make a master array which contains several different user arrays.
[{userID1, userID2}, {userID3, userID4}]
How can I do that in kotlin?
mutableListOf(mutableListOf("yourobject"),mutableListOf("yourobject"))
or
val myList = mutableListOf<MutableList<yourobject>>()
First, declare the master array which will contains other arrays
val masterList = mutableListOf<List<String>>()
Secondly, declare the arrays which will be nested. Assuming userID1, userID2, userID3, userID4 are declared somewhere
val subList1 = listOf(userID1,userID2)
val subList2 = listOf(userID3,userID4)
Finally, add the sublists to the master
masterList.add(subList1)
masterList.add(subList2)
Of course you can do all of this with only one line during masterList instantiation
val masterList = mutableListOf<List<String>>(
listOf(userID1,userID2),
listOf(userID3,userID4)
)

arrayListOf showing Int rather than String - Kotlin

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)

I don't know why array is not recognized when I create a string

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>

Change item's data type in a List or array in Kotlin

how can i change item's data type in arrays or lists in kotlin ?
i found a usual way but i need an easier and faster and better way to change data type of an array :)
fun typeChanger (data:MutableList<Number>): DoubleArray {
val result = mutableListOf<Double>()
for (i in data.iterator()){
result.add(i.toDouble())
}
return result.toDoubleArray()
}
val x = mutableListOf<Number>(+1,+1,-1,-1)
val xx:DoubleArray = typeChanger(x) // It works but i need an easier and faster and better way :)
Array map is your friend. You could keep your function and simplify, or remove it completely as below:-
val xx = x.map { it.toDouble() }
Once it's a list of doubles, you can then leave as a list or use .toDoubleArray() if you need an array.

Categories

Resources