views:

421

answers:

1

Having a problem loading an array into a spinner that is located on a different view. The array is defined properly in arrays.xml with a name of beerstyles. The beerstylespinner is defined as the id of a spinner in carbonationcalculator_view.xml. This works when the code is in the main java class but not the additional carbonationcalculator class. Everything works with the exception of the spinner not being populated with the array.

Here is the code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.carbonationcalculator_view);

    Spinner s = (Spinner) findViewById(R.id.beerstylespinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.beerstyles, R.layout.carbonationcalculator_view);

    adapter.setDropDownViewResource(R.layout.carbonationcalculator_view);

    s.setAdapter(adapter);
}
+1  A: 

I'm not sure this will fix the problem, but there seems to be some confusion on what layout to set for the Adapter's drop down resource. setContentView() should be used for the view you want set for the activity; however, the drop down resource should be what you want each row to look like.

What you should be using is a something like android.R.layout.simple_list_item_1. You can emulate the demo List1.java, but instead of their constructor you would use:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.beerstyles, android.R.layout.simple_list_item_1);

Also, you don't need to call ArrayAdapter.setDropDownViewResource() after using ArrayAdapter.createFromResource() - the third parameter is the drop down view resource.

Daniel Lew
Thanks for your quick response. I tried doing that and the spinner is still not binding to my array. If I use the same code in my main class, it works perfectly. Is there something I should be doing to handle these controls from another view and another class?As a side note, without adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); nothing appears in the spinner drop-down.Thanks for any help with this.
TSDEV
This is what happens when you try and build android applications without fully understanding the language! I realized what I did, I have several views and a menu to open each view. However, that's all I am doing, opening the view, I don't have the intents defined. Will revise and retest, I have a feeling it will work. The code that was supposed to be tied to that view was never even being executed.
TSDEV