Reputation: 1
What difference does it make, if I assign some items using an ArrayAdapter or using android:entries?
Which drawbacks will I face, related to a Spinner functionality?
Adding elements using android:entries
<Spinner
android:id="@+id/edu"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:entries="@array/education"
android:drawSelectorOnTop="true" />"
Adding the items using an ArrayAdapter:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.edu));
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
education_2.setAdapter(arrayAdapter);
Upvotes: 0
Views: 526
Reputation: 10165
If you're curious how an XML attribute is used, the Android source code is your friend. You can see that Spinner
extends AbsSpinner
and that has a constructor defined like this:
public AbsSpinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initAbsSpinner();
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.AbsSpinner, defStyleAttr, defStyleRes);
final CharSequence[] entries = a.getTextArray(R.styleable.AbsSpinner_entries);
if (entries != null) {
final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
context, R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
setAdapter(adapter);
}
a.recycle();
}
As you can see, the code checks if the entries
attribute is set and, if it is, creates a default ArrayAdapter<CharSequence>
to display the given set of strings.
So, to answer your questions:
What difference it makes if I assign items using array adapter or using in build android:entries ?
If you just need to show a list of strings, there's really no difference. The entries
attribute is a convenience if you simply need to display a list of strings in a default spinner view without additional customization.
What drawback I will face related to functionality of spinner?
There's no difference in functionality - it's still a spinner of strings. But you cannot customize the type of item or style of the views in the spinner. So if you need a different layout or more customization in how your items are displayed, you would write the extra code to create and set your own adapter.
Hope that helps!
Upvotes: 0
Reputation: 191738
android:entries
is a static string XML list. You cannot update the Spinner content via Java code. Similarly, (maybe I forget the property, but) in the Java code you are able to use a different layout than android.R.layout.simple_list_item_1
If that's not required for your application, there's no drawback as the XML you have gets roughly translated into the same Java code.
Upvotes: 1