1

I wanted to know the index number of an array so tried as shown below.

val countryCodeList = arrayListOf(*resources.getStringArray(R.array.countryCodes))
val countryIndex = countryCodeList.indexOf("+1")

And it only returns true(0) & false(-1). If string "+1" locates on the 4th row of the list, I want it to return 3 for the result.

How can I receive the index number of an arrayList?

Log

array : [+82, Korea]
result: -1
==================================================
array : [+60, Malaysia]
result: -1
==================================================
array : [+1, USA]
result: 0
4
  • can you please share that countryCodes array. I guess "+1" is at index 0 Commented Sep 23, 2022 at 4:40
  • @AmitBalani Please take a look at the edited posting. It only returns -1 or 0. Commented Sep 23, 2022 at 4:51
  • By looking at this, i can say it's arraylist of arrays. You can't check it with indexOf. Please can you share that countryCodes array which is stored in your res file, not whole file but some of it's items. Commented Sep 23, 2022 at 5:15
  • if your string contains only country codes .. this should work .. pls edit your question and add the values of all Strings .. Commented Sep 23, 2022 at 5:56

3 Answers 3

1

Issue might be, you are not assigning string items to array properly.

Try below code, it should work

Your resource file should like

`<resources>
<string name="app_name">Sample</string>
    <string-array name="countryCodes">
        <item name="KR">@string/korea_number</item>
        <item name="MY">@string/malaysia_number</item>
        <item name="AU">@string/indonesia_number</item>
        <item name="SG">@string/singapore_number</item>
        <item name="CA">@string/us_number</item>
        <item name="NZ">@string/new_zealand_number</item>
    </string-array>

<string name="korea_number">+92</string>
<string name="malaysia_number">+91</string>
<string name="indonesia_number">+99</string>
<string name="singapore_number">+93</string>
<string name="us_number">+94</string>
<string name="new_zealand_number">+98</string> </resources>`

On your activity file or fragment file you can access like this

val countryCodeList = arrayListOf(*resources.getStringArray(R.array.countryCodes)) val countryIndex = countryCodeList.indexOf("+99")

`println("countryIndex $countryIndex")`

if you want, you can replace appropirate country codes.

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

1 Comment

Thanks for the help. It was about the string. I posted my solution.
1

Simply use:

val countries = resources.getStringArray(R.array.countryCodes)
val index = countries.indexOf("+1")

Comments

0

Here I share my final solution for anyone in need. It only returned 0 or -1 because my string was not exactly same as the value on the array list.

Solution

I used filter function and called the value which contains my string. Then, I used replace and deleted "[" and "]". Finally, I could have the exactly same string as the value on array list and now it gives me the index number of the string that I am looking for.

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.