How to obtain android mobile phone contacts and display them in letters (2): android contacts

Source: Internet
Author: User

How to obtain android mobile phone contacts and display them in letters (2): android contacts

The following describes how to categorize and display contacts with the same initials:

In the adapter implement SectionIndexer

In this way, the adapter must implement the following three interfaces:

@Override
public Object [] getSections () {// Collection of sections
The
}

@Override
public int getPositionForSection (int section) {// Position of current section
The
}

@Override
public int getSectionForPosition (int position) {// section information of current position
The
}

So we must process the data of the local contacts
Define one to store section information:

 private class SectionTitle {
    public String title;

    public SectionTitle () {
    title = "";
    }
    The
    public String toString () {
    return title;
    }
    }


Define the following structure to obtain the information needed to implement sectionIndexer:

private class ContactSectionMapper implements SectionIndexer {

private SectionTitle [] mSections = null;
private SparseIntArray mSectionPositionMap = null;
private SparseIntArray mPositionSectionMap = null;

public ContactSectionMapper (SectionTitle [] sectionDatas) {
mSections = sectionDatas;
}
The
public void changeData (SparseIntArray sectionPositionMap, SparseIntArray positionSectionMap) {
mSectionPositionMap = sectionPositionMap;
mPositionSectionMap = positionSectionMap;
}
The
@Override
public Object [] getSections () {
return mSections;
}

/ **
* Find the position corresponding to this section
* /
@Override
public int getPositionForSection (int section) {
if (mSectionPositionMap == null)
return -1;

The
if (section == 0)
return -1;
The
return mSectionPositionMap.get (section, -1);
}

/ **
* Find the section corresponding to this position
* /
@Override
public int getSectionForPosition (int position) {
if (mPositionSectionMap == null)
return -1;

if (position <= 0)
return 0;
The
return mPositionSectionMap.get (position, -1);
}

The
/ **
* @param position
* @return
* /
public boolean isSection (int position) {
if (position == 0)
return true;
The
int sectionIdx = getSectionForPosition (position);
int sectionPosition = getPositionForSection (sectionIdx);

if (sectionIdx == -1 && sectionPosition == -1)
return false;
The
return (position == sectionPosition);
}

public String getSection (int position) {
if (mSections == null)
return NONE_ENGLISH_LETTER_TITLE;

int sectionIndex = getSectionForPosition (position);
if (sectionIndex <0 || sectionIndex> = mSections.length)
return NONE_ENGLISH_LETTER_TITLE;
The
return mSections [sectionIndex] .toString ();
}

}

In the adapter, we overload the changeCursor (which will be called every time the cursor changes) method to process and take out the first letter of data
<span style = "white-space: pre"> </ span> @Override
public void changeCursor (Cursor c) {
processCursor (c);
super.changeCursor (c);
}
The
private void processCursor (Cursor c) {

/ ** define some variables * /
SparseIntArray sectionPositionMap = new SparseIntArray ();
SparseIntArray positionSectionMap = new SparseIntArray ();

for (int i = 0; i <mSectionDatas.length; i ++) {
mSectionDatas [i] .title = "";
}

if (c == null || c.getCount () == 0 || c.isClosed ()) {
mSectionMapper.changeData (sectionPositionMap, positionSectionMap);
return;
}
The
String curtitle = "";

int i = 0;
int position = 0;
while (c.moveToNext ()) {
position = c.getPosition ();

String curLetter = getTitle (getDisplayName (c));

// Look at the first name of the current name, is it the beginning of the section
if (TextUtils.isEmpty (curtitle) ||! TextUtils.equals (curLetter, curtitle)) {
mSectionDatas [i] .title = curLetter;
// The current name is the beginning of the section
sectionPositionMap.put (i, position);
curtitle = curLetter;
i ++;
}
positionSectionMap.put (position, i-1);
}

for (; i <mSectionDatas.length; i ++) {
mSectionDatas [i] .title = curtitle;
sectionPositionMap.put (i, position);
}
mSectionMapper.changeData (sectionPositionMap, positionSectionMap);
}

In the initialization of the adapter:
protected final class ContactsAdapter extends ResourceCursorAdapter implements SectionIndexer, OnScrollListener {

protected boolean mLoading = true;
private ContactSectionMapper mSectionMapper = null;
private SectionTitle [] mSectionDatas = null;
private static final int SECTION_COUNT = 27;
The
public ContactsAdapter (Context context) {
super (context, R.layout.contacts_list_item_photo, null);

mSectionDatas = new SectionTitle [SECTION_COUNT];
for (int i = 0; i <SECTION_COUNT; i ++) {
mSectionDatas [i] = new SectionTitle ();
}
mSectionMapper = new ContactSectionMapper (mSectionDatas);
}
<span style = "white-space: pre"> </ span> @Override
public Object [] getSections () {
return mSectionMapper.getSections ();
}

@Override
public int getPositionForSection (int section) {
return mSectionMapper.getPositionForSection (section);
}

@Override
public int getSectionForPosition (int position) {
return mSectionMapper.getSectionForPosition (position);
}

:



How to find contacts in alphabetical order on Android phones?
If you always click on the screen, a menu will appear.

 
Android how to get the contact information of the address book and display it

Query the database, the general contact information is stored in the database contacts2.db, you can query through ContentProvider, the contact information table structure is more complicated, do not know what kind of information you want to query?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.