Question

I have the following item inside my Spinner: Andrew-15. The first item is the name and second is the age. So I want to split this into two parts where String1 stores Andrew and String2 stores 15. So this is the code which I have

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {

    TextView myText = (TextView) view;
    Toast.makeText(this, "Item selected"+myText.getText(),Toast.LENGTH_SHORT).show();
    String text = spinner.getSelectedItem().toString();
    String[] parts = text.split("-");
    String part1 = parts[0];
    String part2 = parts[1];    
}

But this gives me an error in Log Cat which says:

FATAL EXCEPTION: main
04-28 00:45:58.907: E/AndroidRuntime(18463): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
04-28 00:45:58.907: E/AndroidRuntime(18463):    at com.example.packagename.MainActivity.onItemSelected(MainActivity.java:85)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at android.widget.AdapterView.access$200(AdapterView.java:49)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at android.os.Handler.handleCallback(Handler.java:725)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at android.os.Looper.loop(Looper.java:137)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at android.app.ActivityThread.main(ActivityThread.java:5195)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at java.lang.reflect.Method.invokeNative(Native Method)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at java.lang.reflect.Method.invoke(Method.java:511)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-28 00:45:58.907: E/AndroidRuntime(18463):    at dalvik.system.NativeStart.main(Native Method)

How do I fix this error? Any help will be appreciated. There is a value in my text and it is Andrew-15

Was it helpful?

Solution

// try this way,hope this will help you..

**activity1.xml**code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

**Activity** code
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

        final String[] spinnerValueArray = new String[]{"Andrew-15","Jack-20","John-25","Smith-18","Martin-15"};
        spinner = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, spinnerValueArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String name;
                String version="";
                String [] values = spinnerValueArray[position].split("-");
                if(values.length > 1){
                    name = values[0];
                    version = values[1];
                }else{
                    name = values[0];
                }

                Toast.makeText(MyActivity.this,"Name : "+name+" Age : "+version,Toast.LENGTH_SHORT).show();
            }

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

            }
        });

    }

OTHER TIPS

It says ArrayIndexOutOfBounds. So my guess is That string doesn't have "-". So string array after splitting will only have one item. So when you try to get second item of it, it throws exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top