Created
September 18, 2014 12:33
-
-
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
This file contains hidden or 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
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()); |
This file contains hidden or 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
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