Last active
August 29, 2015 13:57
-
-
Save pwittchen/9400058 to your computer and use it in GitHub Desktop.
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
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(); | |
list.addAll(contacts); | |
} | |
@Override | |
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(android.R.id.text1)).setText(contact.name); | |
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) | |
*/ | |
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) | |
private void highlightItem(int position, View result) { | |
if(position == selectedItem) { | |
// you can define your own color of selected item here | |
result.setBackgroundColor(StringUtils.getColorFromResources(R.color.light_blue)); | |
} else { | |
// you can define your own default selector here | |
result.setBackground(StringUtils.getDrawableFromResources(R.drawable.abs__list_selector_holo_light)); | |
} | |
} | |
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