Using a spinner on another view with a different class defined (Android) - android

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);
}

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.

Related

Why getApplicationContext and Activity context inflate different style of the spinner?

I have a strange problem. I have 2 pre-defined spinner in the xml. However, the item and related dropdown item are rendering in runtime.
However, the following 2 statement provide different result of the layout. I have no idea why the result is like this. But the main difference is getApplicationContext() and this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getSrvNumList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, getSrvNumList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
As #MikeM. commented, it is the issue about the context. Hope the following answer can help someone with this issue.
Use Activity context instead of Application context
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this,
android.R.layout.simple_spinner_item, getSrvNumList());
Otherwise, the application will get different theme to render the spinner.
According to this post (look for Chapter 3), we can consider 2 types of Context: UI-Context (for example getActivity(), view.getContext()...) and non-UI-Context (such as getApplicationContext()...).
When inflate view, let use UI-Context to keep its application theme. That rule is:
* Do you need to access UI related stuff? Use UI-Context.
* Otherwise, Use Non-UI Context.

TactionbarSherlock - having trouble implementing the list navigation

I am trying to work from the ActionbarSherlock example for adding a list navigation. In the example, there is some code like this:
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.list_navigation);
mSelected = (TextView)findViewById(R.id.text);
mLocations = getResources().getStringArray(R.array.locations);
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
}
and that seems to be it. So I am confused where I can add the titles for the navigation. And also it references
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
context, R.array.locations, R.layout.sherlock_spinner_item);
and my code is not aware of R.array.locations and gives a syntax error. But that R.array.locations is not in the examples. Should I create a separate file for it in my layout directory?
Thank you!
Regarding your compile error: Did you create an array resource called "locations"?
/res/values/arrays
<resources>
<string-array name="locations">
<item>Foo</item>
<item>Bar</item>
<item>Baz</item>
</string-array>
</resources>
Regarding providing special "tiles" for the dropdown: Even though it's called "List Navigation", what you are actually seeing in the Action Bar (both the native one and ActionBarSherlock's) is a Spinner. So you can give it any SpinnerAdapter and it will use that.
If you aren't familiar with adapters, I highly recommend you watch this video. The only difference between an adapter for a ListView and an adapter for a Spinner is the getDropDownView() method, which creates the views that appear in the dropdown menu (where getView() provides the view shown in the spinner content area).
But that R.array.locations is not in the examples
It sure is.
<string-array name="locations">
<item>Home</item>
<item>Email</item>
<item>Calendar</item>
<item>Browser</item>
<item>Clock</item>
</string-array>

Is there a simple way to populate a Spinner with an ArrayList?

I have a bunch of ArrayLists filled with data. All I want to do is populate the spinners using the arraylists. In other SO questions people create their own Adapters and store the ArrayList as a field, but I don't need to do that.
I just want to populate the spinner. I don't think theres something this easy, but is there anything resembling
mySpinner.populate( myList );
Simply use the built-in ArrayAdapter, you don't have to write a custom Adapter.
List<String> list = new ArrayList<String>();
// Fill list with strings
...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
You don't specify what data types you are using, but it doesn't matter: you can easily change the ArrayAdapter's subtype to <Integer>, <Double>, or even a custom class <Kittens> (as long as you define the custom classes toString() method.)
ArrayAdapters have an add() method to store things to its internal list. No need to create your own. Try something like this:
for(Object ob : yourArrayList)
<arrayAdapter>.add(ob);
<arrayAdapter>.notifyDataSetChanged();
I would change Object to be whatever collection your List is using.

Is it possible to create an Android spinner components 'ArrayAdapter' with out using the xml layout reference?

In my Android application I create the layout programatically (ie an xml layout file not used for creating the component layout), in this case how can I create a spinner component programatically? Especially the setAdapter portion of spinner component, Is it possible create an ArrayAdapter with out using an XML layout reference like 'android.R.layout.spinner_item'?
Spinner sp_gender = new Spinner(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.spinner_item, new String[] {
"Male",
"Female"
});
sp_gender.setAdapter(spinnerArrayAdapter);
ll_main.addView(sp_gender);
In the above code I want to remove the xml file reference portion 'android.R.layout.spinner_item' because I create the layout completely in the activity class.
No its not possible. Because ArrayAdapter has no default constructor. also u need to specify the structure of the view and also on which it should draw the view thats why arrayadapter takes layout id and context as one of its parameter
ArrayAdapter(Context context, int textViewResourceId) is the constructor of ArrayAdapter with least parameter.

Problems with creating spinner

In my app I'm developing, I have a spinner in an alert dialog box.
The spinner works fine, but when I added the following lines to add the array to the spinner, my app crashing a few seconds after starting up:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.newFileTypeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
newFileType.setAdapter(adapter);
What am I doing wrong?
These Android spinners seem to be a bit complicated as well, I don't think I'll be able to remember how to make them without referencing to the Android docs.
Problem solved.
I realised that the following line:
final Spinner newfiletypespinner = (Spinner) findViewById(R.id.newfiletypespinner);
I had to change to:
final Spinner newfiletypespinner = (Spinner) newFileDialogInflated.findViewById(R.id.newfiletypespinner);
With "newFileDialogInflated" being the previously inflated view so I could have a custom AlertDialog view:
final View newFileDialogInflated = View.inflate(this, R.layout.newfileview, null);
But thanks for the help!
Hard to say for sure whether you've omitted it from your snippet or omitted it from your code, but do you initialize newFileType as a spinner?
Spinner newFileType = (Spinner)findViewById(R.id.newFileTypeSpinner);
or similar? If you're trying to set its adapter before initializing it, that would explain it.

Categories

Resources