【问题标题】:Index out of bounds in a SpinnerSpinner 中的索引超出范围
【发布时间】:2016-03-29 21:45:21
【问题描述】:

有 4 个 Spinnners:
resiko,untung1,untung2,untung3

用户首先选择 resiko 以继续,应用程序将显示哪个 Spinner 可见 (untung1/untung2/untung3)

尝试制作动态数据 Spinner,当您单击第一个 Spinner 内的一个项目时,其他项目将消失,而所需的项目将可见,目前正在工作。
问题是当我从 Spinner resiko 中选择“tinggi”时,会出现错误:java.lang.IndexOutOfBoundsException

我也尝试阅读其他帖子,但仍然不知道该怎么办。
我之前尝试过使用getSelectedItem(),但是当我想从用户选择的可见微调器中获取所需数据时,应用程序本身并没有选择所选项目,而是选择了可见微调器中的第一个数据.
(假设 Spinner 中有 2 个值,A 和 B;用户选择 B,但程序选择 A)

例如:
用户在“resiko”微调器中选择“rendah”,然后下一个可见微调器是 untung2,然后用户在该微调器中选择“sedang”, 但程序选择“--pilih--”而不是“sedang”
这就是为什么我切换到getItemAtPosition(position).toString();

字符串.xml

<string-array name="spinner_resiko_string">
    <item>--Pilih--</item>
    <item>Sangat Rendah</item>
    <item>Rendah</item>
    <item>Sedang</item>
    <item>Tinggi</item>
</string-array>

<string-array name="spinner_return_string">
    <item>--Pilih--</item>
    <item>Rendah</item>
</string-array>

<string-array name="spinner_return_string2">
    <item>--Pilih--</item>
    <item>Rendah</item>
    <item>Sedang</item>
</string-array>

<string-array name="spinner_return_string3">
    <item>--Pilih--</item>
    <item>Rendah</item>
    <item>Sedang</item>
    <item>Tinggi</item>
</string-array>

微调器声明:

    final Spinner resiko = (Spinner) mScrollView.findViewById(R.id.spinner_resiko);
    final Spinner untung1 = (Spinner) mScrollView.findViewById(R.id.spinner_return1);
    final Spinner untung2 = (Spinner) mScrollView.findViewById(R.id.spinner_return2);
    final Spinner untung3 = (Spinner) mScrollView.findViewById(R.id.spinner_return3);

xml 中的微调器:

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_return1"
    android:entries="@array/spinner_return_string"
    android:layout_marginLeft="10dp"/>
<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_return2"
    android:entries="@array/spinner_return_string2"
    android:layout_marginLeft="10dp"/>
<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_return3"
    android:entries="@array/spinner_return_string3"
    android:layout_marginLeft="10dp"/>

代码版本 1(当我在 resiko spinner 中选择“tinggi”时错误索引出界)[使用 getItematPosition]:

 resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
            String resikox = mRelative.getItemAtPosition(position).toString();
            if (resikox.equals("Sangat Rendah")) {
                untung1.setVisibility(View.VISIBLE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
                untungx = untung1.getItemAtPosition(position).toString();
            }
            else if (resikox.equals("Rendah")){
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.VISIBLE);
                untung3.setVisibility(View.GONE);
                untungx = untung2.getItemAtPosition(position).toString();
            }
            else if (resikox.equals("Sedang") || (resikox.equals("Tinggi"))) {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.VISIBLE);
                untungx = untung3.getItemAtPosition(position).toString();
            } else {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }
    });

第 2 版(使用 getItemSelected)

 //set spinner
    resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
            String resikox = mRelative.getItemAtPosition(position).toString();
            if (resikox.equals("Sangat Rendah")) {
                untung1.setVisibility(View.VISIBLE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
                untungx = untung1.getSelectedItem().toString();
            }
            else if (resikox.equals("Rendah")){
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.VISIBLE);
                untung3.setVisibility(View.GONE);
                untungx = untung2.getSelectedItem().toString();
            }
            else if (resikox.equals("Sedang") || (resikox.equals("Tinggi"))) {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.VISIBLE);
                untungx = untung3.getSelectedItem().toString();
            } else {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }
    });

日志猫:

12-23 19:37:23.221  30433-30433/com.example.fabio.tabdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fabio.tabdrawer, PID: 30433
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
        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.AdapterView.getItemAtPosition(AdapterView.java:831)
        at com.example.fabio.tabdrawer.Menu_PIAF$1.onItemSelected(Menu_PIAF.java:183)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:964)
        at android.widget.AdapterView.access$200(AdapterView.java:49)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:928)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5487)
        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:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 您必须分别为每个微调器添加 setOnItemSelectedListener。你的数组大小不一样,这就是你得到异常的原因
  • 我仍然对此感到困惑,你能帮我写代码吗?
  • 很抱歉,我现在编辑了显示微调器声明的帖子

标签: java android android-spinner indexoutofboundsexception


【解决方案1】:

您的&lt;item&gt;--Pilih--&lt;/item&gt; 引起了问题,因为它占据了微调器第一个项目的位置。因此,当您选择最后一项时,Tinggi 会引发索引越界错误。

这将为您提供有关如何在微调器顶部创建不可选择项目的建议:How to make an Android Spinner with initial text "Select One"

您可以将可见性更改为消失,这是个好主意,但是您需要为每个微调器实现单独的 onItemSelectedListeners。 resiko,untung1,untung2,untung3 因为它们是独立的元素并且具有不同大小的数组。

虽然看起来工作量更大,但保持 UI 元素交互分开很重要。

现在如果有一个用户选择通用的方法,这可以模块化。

示例: 因此,如果多个不同的项目选择导致背景颜色在一个名为的方法中变为黄色:

turnBackgroundYellow()

那么这个方法可以放在任意多的on item selected events中。

但是反过来不行。

每个独特的微调器都需要专门为该微调器附加它的侦听器。

制作这些类变量,以便您可以将它们作为参数传递给类方法。

String resikox_;
String untung1_; // and the others

// Create an ArrayAdapter using the string array and a default spinner layout
// Create a separate one for each spinner resiko,untung1,untung2,untung3
ArrayAdapter<CharSequence> adapter = ArrayAdapter
            .createFromResource(getActivity(), R.array.dataobjects_array,
                    android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
resiko.setAdapter(adapter);
// Create a separate one for each spinner resiko,untung1,untung2,untung3
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        resikox_ = parent.getItemAtPosition(position).toString();
        SelectedItemMethod(resikox_)
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        //DO WHATEVER OR NOTHING
                }
});

// Class method to do your item selection stuff.
public void SelectedItemMethod(String item){

    if (item.equals("Sangat Rendah")) {
        untung1.setVisibility(View.VISIBLE);
        untung2.setVisibility(View.GONE);
        untung3.setVisibility(View.GONE);
    }
    //etc, etc for all your spinners or break it up, as you please

【讨论】:

  • 抱歉又问了一些麻烦的事情,请问您可以用我当前的代码举例说明如何为每个微调器实现单独的 onItemSelectedListeners。我知道我现在哪里错了,但不太确定如何通过实施来解决它
  • 是的,你可以教我一个怎么做,非常感谢
  • 是的,我会,再来一个,dataobjects_array是什么,我无法解决这个
  • 请再帮我一次,试着像你给我看的那样做,但我觉得哪里出了问题:stackoverflow.com/questions/34437835/…
【解决方案2】:

为每个微调器添加单独的 setOnItemSelectedListener()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多