Created
October 27, 2014 23:24
-
-
Save jlefler/2bce47fd40b34ac4c4f1 to your computer and use it in GitHub Desktop.
Spinner Adapter helper that allows for a first option not in the underlying adapter. I use it to have a "None" option for spinners backed by CursorAdapters. Simplified to use standard styles and based on the answer HRJ provided at http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.joshlefler.statelaw.ui; | |
import android.content.Context; | |
import android.database.DataSetObserver; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListAdapter; | |
import android.widget.SpinnerAdapter; | |
import android.widget.TextView; | |
/** | |
* Based heavily on the answer provided by HRJ at: | |
* http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one | |
*/ | |
public class OptionalSelectionSpinnerAdapter implements SpinnerAdapter, ListAdapter { | |
protected static final int EXTRA = 1; | |
private final String prompt; | |
protected SpinnerAdapter adapter; | |
protected Context context; | |
protected LayoutInflater layoutInflater; | |
public OptionalSelectionSpinnerAdapter(SpinnerAdapter spinnerAdapter, String prompt, Context context) { | |
this.adapter = spinnerAdapter; | |
this.context = context; | |
this.prompt = prompt; | |
layoutInflater = LayoutInflater.from(context); | |
} | |
@Override | |
public final View getView(int position, View convertView, ViewGroup parent) { | |
// This provides the View for the Selected Item in the Spinner, not | |
// the dropdown (unless dropdownView is not set). | |
if (position == 0) { | |
return getNothingSelectedView(parent); | |
} | |
return adapter.getView(position - EXTRA, convertView, parent); // Could re-use | |
// the convertView if possible. | |
} | |
/** | |
* View to show in Spinner with Nothing Selected | |
* Override this to do something dynamic... e.g. "37 Options Found" | |
* @param parent | |
* @return | |
*/ | |
protected View getNothingSelectedView(ViewGroup parent) { | |
View v = layoutInflater.inflate(android.R.layout.simple_spinner_item, parent, false); | |
((TextView)v.findViewById(android.R.id.text1)).setText(prompt); | |
return v; | |
} | |
@Override | |
public View getDropDownView(int position, View convertView, ViewGroup parent) { | |
if (position == 0) { | |
return getNothingSelectedDropdownView(parent); | |
} | |
// Could re-use the convertView if possible, use setTag... | |
return adapter.getDropDownView(position - EXTRA, convertView, parent); | |
} | |
/** | |
* Override this to do something dynamic... For example, "Pick your favorite | |
* of these 37". | |
* @param parent | |
* @return | |
*/ | |
protected View getNothingSelectedDropdownView(ViewGroup parent) { | |
View v = layoutInflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false); | |
((TextView)v.findViewById(android.R.id.text1)).setText(prompt); | |
return v; | |
} | |
@Override | |
public int getCount() { | |
int count = adapter.getCount(); | |
return count == 0 ? 0 : count + EXTRA; | |
} | |
@Override | |
public Object getItem(int position) { | |
return position == 0 ? null : adapter.getItem(position - EXTRA); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return 0; | |
} | |
@Override | |
public int getViewTypeCount() { | |
return 1; | |
} | |
@Override | |
public long getItemId(int position) { | |
if (position == 0) { | |
return -1; | |
} else { | |
return adapter.getItemId(position - EXTRA); | |
} | |
} | |
@Override | |
public boolean hasStableIds() { | |
return adapter.hasStableIds(); | |
} | |
@Override | |
public boolean isEmpty() { | |
return adapter.isEmpty(); | |
} | |
@Override | |
public void registerDataSetObserver(DataSetObserver observer) { | |
adapter.registerDataSetObserver(observer); | |
} | |
@Override | |
public void unregisterDataSetObserver(DataSetObserver observer) { | |
adapter.unregisterDataSetObserver(observer); | |
} | |
@Override | |
public boolean areAllItemsEnabled() { | |
return false; | |
} | |
@Override | |
public boolean isEnabled(int position) { | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment