Reputation: 68177
I'm experiencing a strange behavior when overriding getDropDownView method of ArrayAdapter
. I need to override this method in order to show correct string value out from my custom object. Thats how my array adapter look like:
ArrayAdapter<NoteType> adapter = new ArrayAdapter<NoteType>(this, android.R.layout.simple_spinner_item, lstNoteTypes){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView lbl = (TextView) super.getView(position, convertView, parent);
lbl.setText(getItem(position).getName());
return lbl;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView lbl = (TextView) super.getView(position, convertView, parent);
lbl.setText(getItem(position).getName());
return lbl;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
So when I override getDropDownView
, my Spinner looks as below - the item height is very small in size and which is not what I want:
But when I comment (or don't override) the getDropDownView
method then it looks fine with the default styling but then I can't inject the required text value into drop down items.
Note the height of items in both images just because of overriding getDropDownView
.
Any suggestions? or what am I missing in my code?
Upvotes: 10
Views: 5951
Reputation: 261
Had to do the same thing so made my own view to be used as checkedTextView
in this the most important thing is setting the height like so android:layout_height="?attr/dropdownListPreferredItemHeight" I was using a base adapter
Upvotes: 1
Reputation: 72553
I think I might know why this happens, but I have to test it, so for now here is another (quick) solution.
Since the Spinner
calls toString()
on every object in the ArrayAdapter
, you could override the toString()
method in your NoteType
class and let it return your String (instead of the default toString()
implementation). This way you wouldn't have to override getDropDownView()
in your adapter, but still have the default styling and your data in it.
Upvotes: 2
Reputation: 5014
I ran into this same problem with a custom BaseAdapter class. This was also mentioned in a comment, but the solution is actually really simple -- just add padding to the TextView you return from the getDropDownView method.
You shouldn't need to add any extra files for this and I was not using ActionBarSherlock (just the default Spinner), so I don't think it's related to that. Here is the code that worked for me, adapted to your example:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Create custom TextView
TextView lbl = (TextView) super.getView(position, convertView, parent);
lbl.setText(getItem(position).getName());
// Add padding to the TextView, scaled to device
final float scale = context.getResources().getDisplayMetrics().density;
int px = (int) (10 * scale + 0.5f);
lbl.setPadding(px, px, px, px);
return lbl;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Create custom TextView
TextView lbl = (TextView) super.getView(position, convertView, parent);
lbl.setText(getItem(position).getName());
// Add padding to the TextView, scaled to device
final float scale = context.getResources().getDisplayMetrics().density;
int px = (int) (10 * scale + 0.5f);
lbl.setPadding(px, px, px, px);
return lbl
}
Upvotes: 1
Reputation: 2844
If you wrote NoteType yourself, override toString() in it. Just add the following to your NoteType-class:
@Override
public String toString() {
return getName();
}
Upvotes: 3