skip to Main Content

I have a Fragment with 12 Spinners and I don’t need to perform any action until the user click on a button.

All my Spinners looks like this.(Showing only 2)

 <Spinner
    android:id="@+id/spinnerP1Type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    style="@style/spinner_style"
    android:entries="@array/powerTypes"
    android:gravity="top"
    />
 <Spinner
    android:id="@+id/spinnerP2Type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    style="@style/spinner_style"
    android:entries="@array/powerTypes"
    android:gravity="top"
    />

All of them have pretty similar name, and there’s no other spinners.
Is possible to process them in a loop instead of creating one new object by each one?

Spinner SpinP1 = getView().findViewById(R.id.spinnerP1Type);
Spinner SpinP2 = getView().findViewById(R.id.spinnerP2Type);

2

Answers


  1. Wrap all the spinners in a ViewGroup (use either LinearLayout or ConstraintLayout) and then when the button is clicked, run a for loop and call getChildAt(loopIndex) on that ViewGroup. The ViewGroup should already have been instantiated with findViewById(R.id.the_container_name)

    The code below

    <LinearLayout
         android:id="@+id/spinner_container"
         android:orientation="vertical"
         `...` >
    
        <Spinner
            android:id="@+id/spinnerP1Type"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            style="@style/spinner_style"
            android:entries="@array/powerTypes"
            android:gravity="top"
            />
         <Spinner
            android:id="@+id/spinnerP2Type"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            style="@style/spinner_style"
            android:entries="@array/powerTypes"
            android:gravity="top"
            />
    
    </LinearLayout>
    

    Then call:

                Linearlayout container = getView().findViewById(R.id.spinner_container);
                
                for (int i = 0; i < container.getChildCount(); i++) {
                  View child = container.getChildAt(i);
                  if (child instanceof Spinner) {
                  Spinner spinner = (Spinner) child;
                  spinner.setOnItemSelectedListener(new SimpleOnItemSelectedListener());
    
                   }
             }
            
            
        /**
         * Listener
         */
        public class SimpleOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
        
           @Override
           public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        
           }
        
           @Override
           public void onNothingSelected(AdapterView<?> adapterView) {
        
           }
        }
    

    Note: SimpleOnItemSelectedListener implements AdapterView.OnItemSelectedListener

    Login or Signup to reply.
  2. One possibility is to create an array of ids to loop over:

    int[] spinnerIds = new int[] {R.id.spinnerP2Type, R.id.spinnerP1Type);
    
    for (int spinnerId : spinnerIds) {
        Spinner spinner = getView().findViewById(spinnerId);
        // etc.
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search