import com.suntek.contact.R;
import com.suntek.contact.model.ContactBean;
import com.suntek.contact.view.QuickAlphabeticBar;
public class ContactListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List list;
private HashMap<String, Integer> alphaIndexer; // 字母索引
private String[] sections; // 存储每个章节
private Context ctx; // 上下文
public ContactListAdapter(Context context, List list,
QuickAlphabeticBar alpha) {
this.ctx = context;
this.inflater = LayoutInflater.from(context);
this.list = list;
this.alphaIndexer = new HashMap<String, Integer>();
this.sections = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
// 得到字母
String name = getAlpha(list.get(i).getSortKey());
if (!alphaIndexer.co
ntainsKey(name)) {
alphaIndexer.put(name, i);
}
}
Set sectionLetters = alphaIndexer.keySet();
ArrayList sectionList = new ArrayList(sectionLetters);
Collections.sort(sectionList); // 根据首字母进行排序
sections = new String[sectionList.size()];
sectionList.toArray(sections);
alpha.setAlphaIndexer(alphaIndexer);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public void remove(int position) {
list.remove(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.contact_list_item, null);
holder = new ViewHolder();
holder.quickContactBadge = (QuickContactBadge) convertView
.findViewById(R.id.qcb);
holder.alpha = (TextView) convertView.findViewById(R.id.alpha);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.number = (TextView) convertView.findViewById(R.id.number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ContactBean contact = list.get(position);
String name = contact.getDesplayName();
String number = contact.getPhoneNum();
holder.name.setText(name);
holder.number.setText(number);
holder.quickContactBadge.assignContactUri(Contacts.getLookupUri(
contact.getContactId(), contact.getLookUpKey()));
if (0 == contact.getPhotoId()) {
holder.quickContactBadge.setImageResource(R.drawable.touxiang);
} else {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
contact.getContactId());
InputStream input = ContactsContract.Contacts
.openConta