2015-12-23 35 views
4

有4個Spinnners:
resiko,untung1,untung2,untung3在索引超出範圍的微調

拾取resiko第一用戶繼續和應用程序將顯示哪個Spinner將是可見的(untung1/untung2/untung3)

試圖製作動態數據微調器,當您點擊第一個微調器內的項目時,其他項目將會消失,並且期望的項目將可見,工作到目前爲止。
問題是,是當我拿起從微調resiko「吉丁宜」,它得到一個錯誤:java.lang.IndexOutOfBoundsException

我也試着閱讀其他職位,但還是不知道應該怎麼辦。
我之前嘗試過使用getSelectedItem(),但是當我想從用戶選擇的可見微調器中獲取所需數據時,應用程序本身並未選取所選項目,而是選擇了可見微調器中的第一個數據。
(假設有一個在所說的紡絲器,A和B 2值;用戶拾取B,但該程序挑選A)

例如在


用戶拾取「rendah」中的「resiko」微調和那麼下一個可見的微調是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微調 「丁宜」)[使用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 
     } 
    }); 

的logcat:

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) 
+0

你必須單獨添加setOnItemSelectedListener每個微調。你的數組大小不一樣,這就是爲什麼你會得到異常 –

+0

我仍然對此感到困惑,你能幫我用代碼先生嗎? –

+0

你能告訴我們你是如何申報和改造你的紡紗廠嗎?這是非常不清楚的。什麼是'untung1',2,3等?事實上,這是另一種語言,因此命名更沒有意義,所以你很難直觀地看到你在做什麼。 –

回答

3

<item>--Pilih--</item>導致問題,因爲它佔用了你的微調的第一個項目的位置。因此,當你選擇最後一個項目Tinggi時,它會拋出索引超出界限的錯誤。

這會給你如何在你的微調頂部創建一個非選擇項建議:How to make an Android Spinner with initial text "Select One"

您可以更改能見度走了,這是一個好主意,但是你需要實現獨立onItemSelectedListeners爲每個微調器。 resiko,untung1,untung2,untung3,因爲它們是獨立的元素,並具有不同大小的陣列。

雖然看起來像更多的工作,但重要的是保持UI元素交互分離。

現在,如果有一種方法對用戶選擇是通用的,則可以將其模塊化。

例如: 所以,如果幾個不同的項目選擇造成的背景色在一個名爲方法變黃:

turnBackgroundYellow()

那麼這種方法可以放置在儘可能多的項目選擇的事件,請你。

但是,相反不起作用。

每個獨特的微調器都需要讓它的偵聽器專門爲該微調器附加它。

創建這些類變量,以便您可以將它們作爲參數傳遞給類方法。

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 
+0

再次提問麻煩的東西很抱歉,我可以請你給我舉一些關於如何爲每個spinner實現單獨的onItemSelectedListeners的代碼。我知道我現在在哪裏錯了,但不知道如何通過實施它來修復它 –

+0

@FabioKhinawan好吧,不是整個代碼,而是告訴你如何去做。 –

+0

是的,你可以告訴我一個如何做到這一點,我非常感謝 –

1

添加獨立setOnItemSelectedListener()每個微調