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>
Related
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.
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.
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.
I am trying to create a list of strings from my arrays.xml file, as found in the android docs too. This is what they use:
ArrayAdapter adapter = ArrayAdapter.createFromResource(context, R.array.colors, android.R.layout.simple_spinner_item);
eclipse is warning that the ArrayAdapter instance is a raw type. Should it really be:
ArrayAdapter<CharSequence> adapter = ...;
?
Thanks
HiIt's always better to leave type checking to java compiler, so it would be better to declare your adapter as
ArrayAdapter<CharSequence> It probably doesn't change anything in your code, especially when you are sure that you are not going to put anything else but strings to your adapter. But it's definately a good practice to use generics to avoid runtime exceptions.Regards!
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.