How to Display Contacts in Android Contact Manager

Java Class to Display Contacts in Android Contact Manager

The below code written in Java for Android Application Development is used to display the saved contact in the contact manager. By using this class you can add and remove the contacts from the contact manager display

Java Code to Display Contact in Android Contact Manager


package edu.bu.CS893;

import java.util.*;
import android.widget.*;
import android.view.*;
import android.content.*;

/**
* This class is used to display contacts in the contact managers' list view.
*
* @author Michael Braude
*/
public class ContactViewAdapter extends BaseAdapter implements
ListAdapter {

/** This is a list of contacts.
*
*/
private List<Contact> mContacts;

/** This hashtable maps contacts to views.
*
*/
private HashMap<Contact, ContactView> mContactToViewMap;

/** This is the context for the view adapter.
*
*/
private Context mContext;

/** Creates the contact view adapter.
*
*/
public ContactViewAdapter(Context context) {
mContacts = new ArrayList<Contact>();
mContactToViewMap = new HashMap<Contact, ContactView>();
mContext = context;
}

/** Adds a contact to the view.
*
* @param contact
*/
public void addContact(Contact contact) {
mContacts.add(contact);
}

/** Removes a contact from the list.
*
* @param contact
*/
public void removeContact(Contact contact) {
mContacts.remove(contact);
mContactToViewMap.remove(contact);
}

/** Returns a contact by its name.
*
* @param contactName
* @return the contact whose name matches.
*/
public Contact findByName(String contactName) {

for (int i = 0; i < mContacts.size(); i++) {
Contact c = mContacts.get(i);
if (c.getName().equals(contactName))
return c;
}

return null;

}

/** Returns true.
*
*/
public boolean areAllItemsSelectable() {
return true;
}

/**
* Always returns true.
*
* @author Michael Braude
*/
public boolean isSelectable(int index) {
return true;
}

/** Returns the count.
*
*/
public int getCount() {
return this .mContacts.size();
}

/** Returns the item at the specified index.
*
*/
public Object getItem(int index) {
return this .mContacts.get(index);
}

/** Returns the ID of the item at the specified index.
*
*/
public long getItemId(int index) {
return index;
}

/** Returns the view at the specified index.
*
* @author Michael Braude
* @param location
* @param convertView
* @param parent
*/
public View getView(int location, View convertView, ViewGroup parent) {

if (location >= this .mContacts.size())
return null;

Contact contact = this .mContacts.get(location);
if (mContactToViewMap.containsKey(contact)) {
return mContactToViewMap.get(contact);
}

ContactView view = new ContactView(mContext, contact);
mContactToViewMap.put(contact, view);
return view;

}

/** Returns the contact view by the contacts name.
*
* @param contactName
* @return
*/
public ContactView getViewByName(String contactName) {
Contact c = this .findByName(contactName);
return (ContactView) getView(this.mContacts.indexOf(c), null,
null);
}

}


Related Topic Android Class to View Contact List
Android: Facebook Contact Manager Layout
What are All Android Phone Native Applications
Android Application Development SDK Dalvik Virtual Machine
What are all the Components of Android Application Architecture
Android  Java  

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback