0

Edit: Not sure why this got downvoted, I'm asking about dynamically making NEW spinners not ones currently in the layout.

I'm trying to figure out the best way to handle generating multiple spinners, with separate adapters and listeners dynamically. Here's the deal, I'm supplying the number of required spinners (which vary depending on category selection from 1-10 additional spinners IE: car make model year, shoes: size,color etc... ) server side. How can I interpret this, create them dynamically, and get user selections? Is this even possible?

I was looking at:

Community
  • 1
  • 1
Petro
  • 3,484
  • 3
  • 32
  • 59

2 Answers2

1

You will need to create a class for creating views like this. But you need to have some sort of a layout for your activity containing an empty LinearLayout or whatever you desire. For example if you create a class for creating a spinner, it should look like this.

    public class MyViewController {
         private Context context;
         public MyViewController(Context context) {
              this.context = context;
         }

         public Spinner getSpinner() {
             return new Spinner(context);
         }

         public ArrayAdapter getAdapter(int resId, String[] values) {
            return new ArrayAdpter(context, resId, values);
         }
    }

Now in your Activity or Fragment or other class you can do something like this.

    public class MainActivity extends Activity {
           private LinearLayout mainLayout;
           private String[] values = {"Hello", "Java", "Android"};
           private String[] values2 = {"Hmmm", "OK", "Bye"};
           private MyViewController myViewController;

           @Override
           protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  mainLayout = (LinearLayout) findViewById
                            (R.id.main_linear_layout);
                  myViewController = new MyViewController(this);
                  LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                  params.setMargins(0, 5, 0, 5);
                  for (int i = 0; i < 10; ++i) {
                        Spinner spin = myViewController.getSpinner();
                        ArrayAdapter adapter;
                        if (i % 2 == 0) {
                          adapter = myViewController.getAdapter(R.layout.my_custom_style, values);
                        } else {
                          adapter = myViewController.getAdapter(R.layout.my_custom_style, values2);
                        }
                        spin.setAdapter(adapter);
                        mainLayout.addView(spin, params);
                  } 

           }
    }
Muneeb Ahmad
  • 134
  • 4
0

Store the values of each spinner on SQLite database so you'll just query it locally.

jotik
  • 17,044
  • 13
  • 58
  • 123
dione llorera
  • 357
  • 4
  • 14
  • That's not what I'm looking for, I'm asking about dynamically making NEW spinners not ones currently on the screen. – Petro Apr 09 '16 at 22:43