1

I have a question and i cant find a solution myself. I am using for example the following string arrays:

     <string-array name="alphabet">
     <item>ccc</item>
     <item>bbb</item>
     <item>aaa</item>
     </string-array>



     <string-array name="ccc">
     <item>1</item>
     <item>2</item>
     <item>3</item>
     </string-array>


     <string-array name="bbb">
     <item>4</item>
     <item>5</item>
     <item>6</item>
     </string-array>


     <string-array name="aaa">
     <item>7</item>
     <item>8</item>
     <item>9</item>
     </string-array>

So, The first string array alphabet is placed inside a spinner. (dropdown menu) Lets say i select CCC, Then i want only the items in between the array CCC to be worked with and stored in an array These information in this array should then be randomized and formatted into groups later on. But i only need a selection of the string selected.

So is there a way to select just one array based on the choice made out of the first array?

Kind regards,

4 Answers 4

1

Here is the code, the way you needed.

Step 1] Set, setOnItemSelectedListener on first spinner and get the Arrayname for target spinner

spinner_alphabates.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
        {

            String arrayName = spinner_alphabates.getSelectedItem().toString();
            int resId = getResources().getIdentifier(arrayName,"array",getPackageName());

            setResultArray(resId);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

Step 2] set the result array on target spinner as

public void setResultArray(int resID)
{
    String [] result_array = getResources().getStringArray(resID);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, result_array);
    spinner_result.setAdapter(adapter);
}

No need to put 'if' 'else' conditions to match array names.

Hope this help you.

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

Comments

0

Try this, I am not sure whether it is going to work, just have try

<string-array name="ccc">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

<integer-array name="id">
    <item> @array/ccc </item>
</integer-array>

2 Comments

Hello sir, Im quite sure it wont work. The problem is i am using a spinner(dropdown) and i want my script to read out which item is selected and then use the array that has the same name.
I would prefer you to use JSON object store somewhere and parse it { "aaa": [ { "0": "0", "1": "1", "2": "2" } ], "bbb": [ { "3": "3", "4": "4", "5": "5" } ], "ccc": [ { "6": "6", "7": "7", "8": "8" } ] } It would be more convinient
0

You need to get array of string like this. and you will access it with index of string array.

String.xml

<string-array name="my_books">
    <item>Item 1</item>
    <item>Item 2</item>
    <item>Item 3</item>
    <item>Item 4</item>
</string-array>

Get List of string in Java file like :

    Resources res = getResources();
    String[] myBooks = res.getStringArray(R.array.my_books);

Comments

0

Try this method and put this method into onCreate in activity method..

change this array add one more item..

<string-array name="alphabet">

    <item>Select</item>
    <item>ccc</item>
    <item>bbb</item>
    <item>aaa</item>
</string-array>

private void initView() {
     spinner = findViewById(R.id.spinner);
    final List<String> asList = Arrays.asList(getResources().getStringArray(R.array.alphabet));
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, asList);
    final ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if (spinner.getSelectedItem().equals("ccc")) {
                adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.ccc)));
                spinner.setAdapter(adapter2);
                adapter2.notifyDataSetChanged();
            } else if (spinner.getSelectedItem().equals("aaa")) {
                adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.aaa)));
                spinner.setAdapter(adapter2);
                adapter2.notifyDataSetChanged();
            } else if (spinner.getSelectedItem().equals("bbb")) {
                adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.bbb)));
                spinner.setAdapter(adapter2);
                adapter2.notifyDataSetChanged();
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

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.