Skip to content

Instantly share code, notes, and snippets.

@lucamtudor
Created September 18, 2014 12:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucamtudor/45269dab23f9bbde2245 to your computer and use it in GitHub Desktop.
Save lucamtudor/45269dab23f9bbde2245 to your computer and use it in GitHub Desktop.
An array adapter that uses the last item as a hint. Use with a spinner
String[] strings = getResources().getStringArray(R.array.spinner_options);
HintAdapter<String> hintAdapter = new HintAdapter<String>(this, android.R.layout.simple_spinner_item, strings);
hintAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(hintAdapter);
mSpinner.setSelection(hintAdapter.getCount());
import android.content.Context;
import android.widget.ArrayAdapter;
import java.util.List;
/**
* Created by Tudor Luca on 18/09/14.
*/
public class HintAdapter<T> extends ArrayAdapter<T> {
public HintAdapter(Context context, int resource) {
super(context, resource);
}
public HintAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
public HintAdapter(Context context, int resource, T[] objects) {
super(context, resource, objects);
}
public HintAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
super(context, resource, textViewResourceId, objects);
}
public HintAdapter(Context context, int resource, List<T> objects) {
super(context, resource, objects);
}
public HintAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public int getCount() {
int count = super.getCount();
// The last item will be the hint.
return count > 0 ? count - 1 : count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment