Set default selection for a spinner
I know this is probably a relativity simple question, but I seem to be overlooking the answer.
In my app, I have a spinner for the user to select the state that they live in. Currently, the app will be focusing on a local audience so I would like to set the default selection to be the state that they live in.
I tried doing:
registration_screen.xml
<Spinner
android:id="@+id/spinHomeState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:entries="@array/state_list"
android:prompt="@string/state_prompt"
android:spinnerMode="dialog"/>
RegistrationScreen.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_screen);
Spinner HomeStateSpinner = (Spinner) findViewById(R.id.spinHomeState);
HomeStateSpinner.setSelection('9');
}
But the app crashes with the following logcat logs:
06-03 13:52:44.512
1736-1736/com.name.of.app E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.hollywoodcert.soupserver.hollywoodcert, PID: 1736 java.lang.IndexOutOfBoundsException: Invalid index 57, size is 50 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.Arrays$ArrayList.get(Arrays.java:66) at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337) at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390) at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) at android.widget.Spinner.makeView(Spinner.java:592) at android.widget.Spinner.layout(Spinner.java:540) at android.widget.Spinner.onLayout(Spinner.java:502) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660) at android.widget.TableRow.onLayout(TableRow.java:123) at android.view.View.layou t(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.TableLayout.onLayout(TableLayout.java:448) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.widget.ScrollView.onLayout(ScrollView.java:1468) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493) at android. view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744) at android.view.Vi ewRootImpl.doTraversal(ViewRootImpl.java:1000) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
I would appreciate any advice.
According to the android docs, the Spinner method setSelection
receives either an int
or int
and boolean
arguments.
In your code your calling HomeStateSpinner.setSelection('9');
passing a char
as argument. Another thing is that the java.lang.IndexOutOfBoundsException
thrown means that you're trying to access a position outside of the array set on the spinner.
My sugestion is first to try and call the method like so:
HomeStateSpinner.setSelection(9);
And if that doesn't work, try calling it with a lower value like:
HomeStateSpinner.setSelection(2);
Use this:
String compareValue = "9";
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_layout, list);
dataAdapter.setDropDownViewResource(R.layout.spinner_layout);
rooms.setAdapter(dataAdapter);
if (!compareValue.equals(null)) {
int spinnerPostion = dataAdapter.getPosition(compareValue);
HomeStateSpinner.setSelection(spinnerPostion);
spinnerPostion = 0;
}
if 9 is the string use above solution,else 9 is the index/position at which you want to set selected then just use:
HomeStateSpinner.setSelection(9);
This must solve your problem.
链接地址: http://www.djcxy.com/p/93156.html上一篇: 凌空单身中的空指针异常
下一篇: 为微调器设置默认选择