0

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:

enter image description here

What am I doing wrong?

4 Answers 4

3

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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,
        
    )

Comments

0

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) 

Comments

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.