简体   繁体   中英

Populate Spinner based on Selection ArrayIndexOutOfBoundsException

I am using same String array to populate data into two spinners , like this:

String[] arrayMonths = { "January", "Feburary", "March", "April", "May",
        "June", "July", "August", "September", "October", "November","December" };

adapter = new ArrayAdapter<String>(MainActivity.this, 
            android.R.layout.simple_spinner_item, arrayMonths);

spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);

Now, I want whenever i make selection in spinner1, like i have selected August then show September as initial value in spinner2 (And also remove all the previous months in spinner2 ).

In a same way, if i selected October in spinner1 , then start spinner2 with November

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        spinner1.getItemAtPosition(position).toString(), Toast.LENGTH_LONG)
                        .show();
            }

UPDATE - As suggested by @AMY

    public class MainActivity extends AppCompatActivity {

    Spinner spinner1, spinner2;
    ArrayAdapter<String> adapter;   

    String newArr[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);

        final String[] arrayMonths = { "January", "Feburary", "March", "April", "May",
        "June", "July", "August", "September", "October", "November","December" };

        newArr = new String[] {"January", "Feburary", "March", "April", "May",
                "June", "July", "August", "September", "October", "November","December"};

        adapter = new ArrayAdapter<String> (MainActivity.this, 
                android.R.layout.simple_spinner_item, arrayMonths);

        spinner1.setAdapter(adapter);

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        spinner1.getItemAtPosition(position).toString(), Toast.LENGTH_LONG)
                        .show();

                newArr = new String[arrayMonths.length-1];
                System.arraycopy(arrayMonths , position, newArr , 0, arrayMonths.length);
                adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, newArr);
                spinner2.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

    }
}

Getting Exception: ArrayIndexOutOfBoundsException

 java.lang.ArrayIndexOutOfBoundsException: src.length=12 srcPos=0 dst.length=11 dstPos=0 length=12

OnItemSelect copy the your months array from selected postion to last position in new array. And set that new array to snd Spinner

     spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view,
                            int position, long id) {
                      String[] newArr = new String[arrayMonths.length-1];
                      System.arraycopy(arrayMonths , position, newArr , 0, arrayMonths.length);
                      adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, newArr);
                      spinner2.setAdapter(adapter);
                      spinner2.notifyDataSetChanged();
                    }

The problem in your code at these lines :

newArr = new String[arrayMonths.length]; 
System.arraycopy(arrayMonths, position, newArr, 0, arrayMonths.length);

You need to define the array size as arrayMonths.length - (selected_position+1) .

Moreover, Check the Signature of method arraycopy :

src : the source array to copy the content.

srcPos : the starting index of the content in src.

dst : the destination array to copy the data into.

dstPos : the starting index for the copied content in dst.

length : the number of elements to be copied.

So, you need to change the length of elements to be copied as arrayMonths.length-selected_position .

Code snippet

int nextPosition = position + 1;
int newSize= arrayMonths.length-nextPosition ;
newArr = new String[newSize]; 
System.arraycopy(arrayMonths, nextPosition , newArr, 0, newSize);

Hope it helps you ツ

Yes you can do that : In your Spinner1's onItemSelected method you can write like below

spinner2.setSelection(position+1);

here you make sure that if the index is last it will throw a IndexOutOfBoundExcecption so handle that.

On your spinner1:

    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
               if(position<arrayMonths.length-1){
                   spinner2.setSelection(position+1)
                }
                else{
                    //set spinner 2 selection to january if spinner1 selects december.
                   spinner2.setSelection(0);
                 }
            }
       }

try this:

 spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                spinner2.setSelection((position + 1) % 12);

            }

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

            }
        });

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position>0) {
                    spinner1.setSelection((position - 1) % 12);
                }else{
                    spinner1.setSelection(11);
                }

            }

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

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM