Contacting Our Friends
Then, we need to modify TimelineEntryWrapper so it looks at the isContact
property of TimelineEntry and sets the background accordingly, so change
populateFrom() in TimelineEntryWrapper to be:
void populateFrom(TimelineEntry s) {
getFriend().setText(s.friend);
getCreatedAt().setText(s.createdAt);
getStatus().setText(s.status);
if (s.isContact) {
getFriend().setBackgroundColor(0xFF0000FF);
}
else {
getFriend().setBackgroundColor(0x00000000);
}
}
You might think that we only need to set the background when the friend is
a contact. However, since rows get recycled, if we do not reset the
background when the friend is not a contact, soon all our rows will be
highlighted, mostly in error.
Step #3: Find and Highlight Matching Contacts
Of course, our isContact property in TimelineEntry is always set to be false,
which is not terribly helpful. Instead, we need to do a lookup to see if a
friend is actually a contact, so we can set that flag appropriately. We do not
need any data about the contact – all we need to know is if there exists a
contact with the appropriate Twitter "organization" and screen name.
First, add the READ_CONTACTS permission to the AndroidManifest.xml file for
Patchy. Without this, we cannot find out if a friend is a contact.
Next, add a PROJECTION to Patchy:
private static final String[] PROJECTION=new String[] {
ContactsContract.Contacts._ID,
};