0

I have positions like [1, 2, 3, 4]

I'm fetching array list from res/values/somearray.xml

My question is how can I get the string values from "res/xml/somearray.xml" array using the positions

So far my code:

    String checkedids = String.valueOf(group.getCheckedIds()); //group.getCheckedIds() returns Set<Integer> positions basically

for (int i = 0; i < checkedids.length(); i++) {
    if (checkedids.contains("1")) {
        if (!TextUtils.isEmpty(valuez)) {
            valuez = valuez + "Fashion";
        } else {
            valuez = "Fashion";
        }
    } else if (checkedids.contains("2")) {
        if (!TextUtils.isEmpty(valuez)) {
            valuez = valuez + "Lifestyle";
        } else {
            valuez = "Lifestyle";
        }
    }l

}

Result is not accurate with some repeated strings.

My arraylist.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="dummy_text">
        <item>Fashion</item>
        <item>Lifestyle</item>
        <item>Beauty and Makeup</item>
        <item>Parenting and Kids</item>
        <item>Pets</item>
    </string-array>

</resources>
3
  • Does this answer your question? Android loading string arrays programmatically Commented Jun 12, 2021 at 8:22
  • @AnantaRaha No that's quite different Commented Jun 12, 2021 at 8:28
  • Check out my answer. Did you mean this? Commented Jun 12, 2021 at 8:41

2 Answers 2

0

You need to be clear about various concepts here. Be clear about the code you write before just writing it. I'll give you the correct solution but you have to infer on what you have done wrong.

You can directly iterate over the set of integers for IDs. The usage of String.valueOf seems wrong here.

Set<Integer> checkedIds = group.getCheckedIds();
String[] dummyArrayValues = getResources().getStringArray(R.array.dummy_text);

for(String id: checkedIds){
    if(id > 0 && id <= dummyArrayValues.length){
         if (!TextUtils.isEmpty(valuez))
            valuez = valuez +" "+ dummyArrayValues[id-1]; //Array indexing is zero based so using id-1 to get the correct position according to your example
         else
            valuez = dummyArrayValues[id-1];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have 3 columns in a row. When I'm clicking on first item of a row I'm getting the whole 3 columns item value in result
0

Possibly you are trying to achieve like following:

Set<Integer> checkedids = group.getCheckedIds(); //group.getCheckedIds() returns Set<Integer> positions basically
String[] values = getResources().getStringArray(R.array.dummy_text);
for (Integer i : checkedids) {
    String stored = values[i-1];
    if (!TextUtils.isEmpty(valuez)) {
        valuez = valuez + stored;
    } else {
        valuez = stored;
    }
}

4 Comments

Suppose when I click on fashion the result value in logcat I'm getting is this. FashionLifestyleBeauty and Makeup It should return only "Fashion"
How did you select the positions in your click event? If you have the positions, no need to iterate over the whole array, just values[position-1] will give you that string.
"no need to iterate over the whole array, just values[position-1] will give you that string" Any practical implementation of this?
Since your post/question seems unclear, it depends on how you handle click from your Views. For example, in your "Fashon" View onClickListener, you can use values[0]; for "Lifestyle", use values[1] etc.

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.