Hi,

I managed to display contact name in listbox and i created one array to save only contact number. i try to read contact number based on current item index in listbox but i found the number and name are mismatch.

For example, contact name Aza is on index 15 but the number showing is belong to index 18. How to solve this problem? Is't because of the sorting?

Here is my code:

void CSMSExampleAddressContainer::GetContact()
{
CTextListBoxModel* model = iAddListBox->Model();
CDesCArray* NumberArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
TInt selectedIndex = iAddListBox->CurrentItemIndex();

// try to display contact number based on contact name
CAknNoteDialog* note = new (ELeave) CAknNoteDialog (CAknNoteDialog::EConfirmationTone,
CAknNoteDialog::ENoTimeout);
note->PrepareLC(R_CONFIRMATIONNOTE_DISPLAY_NOTE);
note->SetTextL ((*iNumberArray)[selectedIndex]);
note->RunLD();
}

void CSMSExampleAddressContainer::InitFromContactsDbL()
{

//Get the ContactIdArray from the Database - caller does
//not take ownership, so don't push it onto the CleanupStack

const CContactIdArray* contacts = iDatabase->SortedItemsL();

const TInt nc = contacts->Count();

for (TInt i=0;i<=nc-1;i++) //For each ContactId
{
CContactItem* contact=NULL;
HBufC* firstNameBuf=NULL;
HBufC* lastNameBuf=NULL;
HBufC* numberBuf=NULL;

//Open the contact and get its fieldset
contact = iDatabase->OpenContactL((*contacts)[i]);
CleanupStack::PushL(contact);

CContactItemFieldSet& fieldSet = contact->CardFields();

//The index of the field in the Contact fieldset
TInt index;

// Get Phone Number
TInt lastind = -1;
index = fieldSet.Find(KUidContactFieldPhoneNumber);

TInt k=0;

const TInt fieldCount = fieldSet.Count();

while(!(index < 0) || (index >= fieldCount))
{
k++;
CContactItemField& phonenoField = fieldSet[index];
CContactTextField* phoneno = phonenoField.TextStorage();

numberBuf = phoneno->Text().AllocLC();
_LIT(KSpaceDes," ");
TInt bufLen = (numberBuf->Length()-1);
TInt sq=0;
while (sq<=bufLen)
{
if (numberBuf->Des().Mid(sq,1)==KSpaceDes)
{
numberBuf->Des().Delete(sq,1);
bufLen--;
}
else
sq++;
}

if (numberBuf->Length()>0)
{
// Get first name.
TInt findpos = fieldSet.Find(KUidContactFieldGivenName);
// Check that the first name field is actually there.
if (!(findpos < 0) || (findpos >= fieldSet.Count()))
{
CContactItemField& firstNameField = fieldSet[findpos];
CContactTextField* firstName = firstNameField.TextStorage();
firstNameBuf = firstName->Text().AllocLC();
}

// Get last name.
findpos = fieldSet.Find(KUidContactFieldFamilyName);
// Check that the last name field is actually there.
if (!(findpos < 0) || (findpos >= fieldSet.Count()))
{
CContactItemField& lastNameField = fieldSet[findpos];
CContactTextField* lastName = lastNameField.TextStorage();
lastNameBuf = lastName->Text().AllocLC();
}

TInt len = numberBuf->Length();
if (firstNameBuf) len+=firstNameBuf->Length();
if (lastNameBuf) len+=lastNameBuf->Length();

HBufC* combinedDes = HBufC::NewLC(len+10);
// try on 9/10
HBufC* numberDes = HBufC::NewLC(len+10);

_LIT(KListItemFormatter,"%S %S");
_LIT(KListOItemFormatter,"%S");

// Format to display contact name
if ((firstNameBuf)&&(lastNameBuf))
combinedDes->Des().Format(KListItemFormatter,firstNameBuf,lastNameBuf);
else
{
if (firstNameBuf)
combinedDes->Des().Format(KListOItemFormatter,firstNameBuf);
else if (lastNameBuf)
combinedDes->Des().Format(KListOItemFormatter,lastNameBuf);
}

numberDes->Des().Format(KListOItemFormatter,numberBuf);
iNumberArray->InsertL(0,*numberDes);

iNumberArray->Sort();
CleanupStack::PopAndDestroy(numberDes);


iAllRecords->InsertL(0,*combinedDes);
iTextArray->InsertL(0,*combinedDes);

// Sorting contact name
iTextArray->Sort();
iAllRecords->Sort();


CleanupStack::PopAndDestroy(combinedDes);

if (lastNameBuf) CleanupStack::PopAndDestroy(lastNameBuf);
if (firstNameBuf) CleanupStack::PopAndDestroy(firstNameBuf);

}

if (numberBuf) CleanupStack::PopAndDestroy(numberBuf);

lastind = index;
if (lastind<=fieldSet.Count()-1)
{
index = fieldSet.FindNext(KUidContactFieldPhoneNumber,lastind+1);
if (index==lastind) break; //i.e. break while
}
else
break;
}

//aSortOrder->Reset();
iDatabase->CloseContactL(contact->Id());
CleanupStack::PopAndDestroy(contact);
}
}