为微调器设置默认选择
我知道这可能是一个相对简单的问题,但我似乎忽略了答案。
在我的应用程序中,我有一个微调器供用户选择他们所在的状态。目前,该应用程序将专注于本地观众,因此我想将默认选择设置为他们所处的状态。
我试着做:
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');
}
但该应用程序崩溃与以下logcat日志:
06-03 13:52:44.512
1736-1736 / com.name.of.app E / AndroidRuntime:致命异常:主进程:com.hollywoodcert.soupserver.hollywoodcert,PID:1736 java.lang.IndexOutOfBoundsException:索引57无效,java.util中的大小为50。 Array.RowIndexOutOfBoundsException(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)在android.view.View.layout(ViewGroup.java:4631)上的android.view.View.layout(View.java:14817)上的android.widget.Spinner.onLayout(Spinner.java:502)。 widget.LinearLayout.setChildFrame(LinearLayout.java:1671)在android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)在android.widget.TableRow.onLayout(TableRow.java:123)在android.view.View.layou 在android.widget.Lineout.java:1671 android.widget.LineoutLayout.layoutVertical(LinearLayout.java)android.view.ViewGroup.layout(ViewGroup.java:4631)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)的t(View.java:14817) :1525)在android.view.View.layout(ViewGroup.java:4631)android.view.View.layout(View.java:14817)android.widget.TableLayout.onLayout(TableLayout.java:448)在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。在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)上android.view.View.Group.layout(ViewGroup.java:4631) :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)在android。 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)android.widget.FrameLayout.onLayout上的android.view.ViewGroup.layout(ViewGroup.java:4631)上的view.View.layout(View.java:14817) (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)android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)在android.view.View.layout(View.java:14817)在android。 view.ViewGroup.layout(ViewGroup.java:4631)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)在android.widget.FrameLayout.onLayout(FrameLayout.java:388)android.view.View.layout (ViewRootImpl.performTraversals(ViewRootImpl.java:android.view.java:14817)at android.view.View.Group.layout(ViewGroup.java:4631)at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java: 1744)在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) )在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)
我将不胜感激任何建议。
根据android文档,Spinner方法setSelection
接收int
或int
和boolean
参数。
在你的代码中调用HomeStateSpinner.setSelection('9');
传递一个char
作为参数。 另一件事是抛出的java.lang.IndexOutOfBoundsException
意味着你正在尝试访问微调器上的数组集以外的位置。
我的消化首先要尝试调用这样的方法:
HomeStateSpinner.setSelection(9);
如果这不起作用,请尝试用较低的值调用它:
HomeStateSpinner.setSelection(2);
用这个:
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;
}
如果9是上面的解决方案使用的字符串,否则9是要设置选择的索引/位置,然后使用:
HomeStateSpinner.setSelection(9);
这必须解决你的问题。
链接地址: http://www.djcxy.com/p/93155.html上一篇: Set default selection for a spinner
下一篇: ListView Not Working after Fragment Transaction detach and attach?