Difference between getView & getDropDownView in SpinnerAdapter
If we look at the following code, we have name and value array in getView and getDropDownView.
private void initView() {
SpinnerDropDownAdapter sddadapter = new SpinnerDropDownAdapter(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sddadapter.name);
Spinner getViewSP = (Spinner) findViewById(R.id.getview_sp);
getViewSP.setAdapter(adapter);
Spinner getViewWDropDownSP = (Spinner) findViewById(R.id.getview_w_drop_down_sp);
getViewWDropDownSP.setAdapter(sddadapter);
}
static class SpinnerDropDownAdapter extends BaseAdapter implements
SpinnerAdapter {
Context context;
SpinnerDropDownAdapter(Context ctx) {
context = ctx;
}
String[] name = { " One", " Two", " Three", " Four", " Five", " Six",
" Seven", " Eight" };
String[] value = { " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };
@Override
public int getCount() {
return name.length;
}
@Override
public String getItem(int pos) {
// TODO Auto-generated method stub
return name[pos];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(context);
text.setTextColor(Color.BLACK);
text.setText(name[position]);
return text;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView text = new TextView(context);
text.setTextColor(Color.BLACK);
text.setText(value[position]);
return text;
}
}
If getDropDownView method is not implemented, the drop down pop up will get the view from getView. Thus, it will only show name.
When both getView and getDropDownView is implemented, the former getting name and the latter getting value, the spinner at rest will get name from getview and the drop down pop up will get value.
Same code on getView and getDropDownView on ANDROID spinner
call getView from your getDropDownView and return result
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getView(position, convertView, parent);
}
Override of getDropDownView in ArrayAdapter in order to change color of particular row in dropdown also change color of another rows
I found the problem. Here is the correct version.
final ArrayAdapter<CharSequence> sprache_ratoromanisch_adapter =
new ArrayAdapter<CharSequence>(
getContext(),
android.R.layout.simple_spinner_item,
list_sprache_ratoromanisch) {
@Override
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row = null;
if(position == 0) {
row = super.getDropDownView(position, convertView, parent);
row.setBackgroundColor(Color.LTGRAY);
}
else {
row = super.getDropDownView(position, null, parent);
}
return row;
}
};
Android Spinner with different layouts for drop down state and closed state?
You have to create a custom Adapter
class for the Spinner and overwrite the two methods getView()
for the normal closed view and getDropDownView()
for the drop down list view. Both methods must return a View
object for a single element.
Have a look at this tutorial it might help you getting started.
How to correctly overwrite methods of SpinnerAdapter
Thanks for the answers, but nevertheless I want to share with you the solution I used after searching more references:
Instead that just implementing SpinnerAdapter in my Adapter, I extend BaseAdapter and implement SpinnerAdapter:
private class ListAdapter extends BaseAdapter implements SpinnerAdapter {
@Override
public int getCount() {
return allLists.size();
}
@Override
public Object getItem(int position) {
return allLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(allLists.get(position).getName());
return text;
}
}
Then it isn't necessary to override all this strange methods such as isEmpty(), registerDataObserver(), etc.
And if necessry one can still override getDropDownView(...)
Additionally using this solution, one can call adapter.notifyDatasetChanged() which isn't as easy if the adapter just implements SpinnerAdapter and doesn't extend BaseAdapter.
Spinner usage on Android
You need to set getView() and getDropDownView() in your adapter.
getView() will set the layout for your picture 1, and getDropDownView() sets the -as the name says- drop down view for your picture 4.
Check this well written answer
Android data binding spinner adapter
It's actually not a databinding-issue. It's because TextView arent wrapped by default and also doesnt have elipses if you dont set them.
You may want to try either to create your own Layout which gets inflated, or you may use
textView.setSingleLine(false)
textView.setMaxLines(2)
If this doesnt work you need to overwrite getDropDownView
@Override
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(_context);
}
TextView item = (TextView) convertView;
item.setText(yourItems.getItem(position));
final TextView finalItem = item;
item.post(new Runnable() {
@Override
public void run() {
finalItem.setSingleLine(false);
}
});
return item;
}
Easiest solution is just switch your Spinner using dialog design by putting this into your xml.
<Spinner android:spinnerMode="dialog" />
Related Topics
How to Get Data from Dialogfragment to a Fragment
Android Tablayout Android Design
Android - Creating a New Activity in Eclipse
How to Enable Internal App Sharing for Android
Successful Share Intent for Android
Should I Use Getapplicationcontext or Activity.This in a Long Running Asynctask
How to Kill All Running Applications in Android
Sethinttextcolor() in Edittext
Converting from Glsurfaceview to Textureview (Via Gltextureview)
How to Detect Android Listview Scrolling Stopped
Android Active Link of Url in Textview
How to Import Eclipse Library Project from Github to Android Studio Project
Should The Call to The Superclass Method Be The First Statement
Convert Image to PDF in Android
How to Make a Portion of a Checkbox's Text Clickable