Symbian电话簿的增删改查

//注意添加工程的权限!!!
添加的头文件
LIBRARY aknnotify.lib eiksrv.lib//note
LIBRARY PbkEng.lib  //CPbkContactEngine.h
LIBRARY cntmodel.lib         //For Phon ebook

#include <cpbkcontactengine.h>


//获取元素
void CWorldContainer::AddInfoToList()
{   
    //创建引擎类
    CPbkContactEngine* pPbkContactEngine = CPbkContactEngine::NewL();
    //查询所有的记录
    _LIT(KEmpty, "");
    CContactIdArray* pArrayOfID = pPbkContactEngine->FindLC(KEmpty);
    //得到记录总数
    TInt nCount = pArrayOfID->Count();
    //循环读取每一条记录
    TInt i = 0;
    for(i = 0; i<nCount; i++)
    {
        //创建记录变量
        CPbkContactItem*  pContactItem = pPbkContactEngine->ReadContactLC((*pArrayOfID)[i]) ;
        //记录集为空就执行下一条记录
        if (pContactItem == NULL)
        {
            continue;
        }
        //得到姓名
        TPbkContactItemField* pCntField = pContactItem->FindField(EPbkFieldIdFirstName);
       
        TBuf<32> sBuf(KNullDesC);
        sBuf.AppendNum(iListBoxItems->Count());
        sBuf.Append(_L("/t"));
        //开辟姓名空间
        HBufC * pFirstName = HBufC::NewMaxL(20);
        //清空
        pFirstName->Des().Zero();
        TPtr ptr(pFirstName->Des());
        //得到内容
        pCntField->GetTextL(ptr);

        //添加到显示文本中
        sBuf.Append(pFirstName->Des());
        //做相应的操作
        iListBoxItems->AppendL(sBuf);
        //删除域
        delete pFirstName;
        //记录集出栈
         CleanupStack::PopAndDestroy(pContactItem);
    }
    CleanupStack::PopAndDestroy(pArrayOfID);   
    delete pPbkContactEngine;


    iListbox->HandleItemAdditionL();
}



//增加元素
void CWorldContainer::AddItem()
{
    //创建引擎类
    CPbkContactEngine* pPbkContactEngine = CPbkContactEngine::NewL();
    CPbkContactItem* pContactItem = pPbkContactEngine->CreateEmptyContactL();
    CleanupStack::PushL(pContactItem);
    TPbkContactItemField* pField = NULL;

    pField = pContactItem->FindField(EPbkFieldIdLastName);
    if(pField)
    {
        pField->TextStorage()->SetTextL(_L("wang"));
    }

    //firstName
    pField = pContactItem->FindField(EPbkFieldIdFirstName);
    if(pField)
    {
        pField->TextStorage()->SetTextL(_L("shenghu"));
    }
    //telephone       
    pField = pContactItem->FindField(EPbkFieldIdPhoneNumberGeneral);
    if(pField)
    {
        pField->TextStorage()->SetTextL(_L("13025698512"));
    }

    pPbkContactEngine->AddNewContactL(*pContactItem);
   
    CleanupStack::PopAndDestroy(pContactItem);

    delete pPbkContactEngine;
 
}


//删除元素
void CWorldContainer::DeleteItem()
{
    //创建引擎类
    CPbkContactEngine* pPbkContactEngine = CPbkContactEngine::NewL();
   
    _LIT(KEmpty, "");
    CContactIdArray* pArrayOfID = pPbkContactEngine->FindLC(KEmpty);
    pPbkContactEngine->DeleteContactsL(*pArrayOfID);
    CleanupStack::PopAndDestroy();


    delete pPbkContactEngine;
   

}

你可能感兴趣的:(Symbian电话簿的增删改查)