Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public class ContactsAdapter extends BaseAdapter {
private final ArrayList list;
private int selectedItem = -1; // no item selected by default
// put neccessary code here - it's not important in this description
public ContactsAdapter(List<Contact> contacts) {
list = new ArrayList();
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
} else {
result = convertView;
highlightItem(position, result);
Contact contact = getItem(position);
((TextView) result.findViewById(;
return result;
* methods from StringUtils calls:
* getContext().getResources().getColor(int resourceId)
* getContext().getResources().getDrawable(int resourceId)
* You can use them in your own context
* (e.g. generic application context or you can pass activity context)
private void highlightItem(int position, View result) {
if(position == selectedItem) {
// you can define your own color of selected item here
} else {
// you can define your own default selector here
public void setSelectedItem(int selectedItem) {
this.selectedItem = selectedItem;
// put rest of your necessary code here...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.