D. Aji Pratama D. Aji Pratama - 2 months ago 10
Android Question

a simple way to declare a spinner with array

I want to make some dynamically added

Spinner
on android. When First
Spinner
is selected, the second spinner will appear. I want to create all the
Spinner
programatically.

Here's what I've done so far.

public class MainActivity extends Activity {

Spinner spnKolom[]; // I declare an array of Spinner

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}


So I tried creating the Spinners like this.

final DataHelper db = new DataHelper(getApplicationContext());
final List<String> labelKolom = db.getKolom();

for (int itung=0;itung<labelKolom.size();itung++){
spnKolom[itung] = new Spinner(this);
}


What is the right way to do this?

Edit

My problem is on declaring a spinner with array. In my expetation, I want to declare one Spinner with array to make many spinner, so I can use it to create a New Spinner when First spinner is selected.

Answer

As per my understanding of your requirement I have done below coding, so please check it, hope it will help you.

First Implement Listner in your Activity

    public class MainActivity  extends Activity implements AdapterView.OnItemSelectedListener

Now Your XML file should look like below

activity_main.xml

    <LinearLayout
        android:id="@+id/main_Layout"
        android:background="#FF0000"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ScrollView
            android:id="@+id/scMainView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/llSpinnerSection"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

Now in your main activity declare your layout as below

   LinearLayout  mainSpinnerLayout = (LinearLayout) findViewById(R.id.llSpinnerSection);

Now Create One Method For Getting Data And Set Spinner

   public void createSpinners()
   {

      final DataHelper db = new DataHelper(getApplicationContext());
      final List<String> labelKolom = db.getKolom();

      spnKolom = new Spinner[labelKolom.size()];

      for (int itung=0;itung<labelKolom.size();itung++)
      {
           spnKolom[itung] = new Spinner(this);
           spnKolom[itung].setTag(itung);

           ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labelKolom);
           spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spnKolom[itung].setAdapter(spinnerArrayAdapter);
          spnKolom[itung].setOnItemSelectedListener(this);

          if(itung == 0)
          {
             spnKolom[itung].setVisibility(View.VISIBLE);
          }
          else
          {
             spnKolom[itung].setVisibility(View.GONE);
          }

          mainSpinnerLayout.addView(spnKolom[itung]);
     }
}

And Finally Implement Methods For Spinner To Show Next Spinner on Item Selection

//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {

    if(position < spnKolom.length - 1)
    {
        spnKolom[position + 1].setVisibility(View.VISIBLE);
    }

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}

Hope this will work for you