You Are Here:

Community: Wiki

This page was last modified on 27 September 2009, at 13:00.

List box with Search, example

From Forum Nokia Wiki

Reviewer Approved   

The CSearchListContainer illustrates how to use CAknSearchField with list boxes to allow users to search the list items. To use this example modify the GetArrayL() method and add code to populate the text item array for the list box as well as add icons to the list box icon array. The text string format for the CAknSingleGraphicStyleListBox is “1\tText “, where 1 is a zero based index for the icon array.

If you want to use a list box without the search box option you can use the List box example instead.

SearchListbox.cpp

CSearchListContainer* CSearchListContainer::NewL(void)
{
CSearchListContainer* self = CSearchListContainer::NewLC();
CleanupStack::Pop(self);
return self;
}
 
CSearchListContainer* CSearchListContainer::NewLC(void)
{
CSearchListContainer* self = new (ELeave) CSearchListContainer();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
CSearchListContainer::~CSearchListContainer()
{
delete iFindBox;
iFindBox = NULL;
 
delete iListBox;
}
 
void CSearchListContainer::ConstructL()
{
CreateWindowL();
 
SetRect(CEikonEnv::Static()->EikAppUi()->ClientRect());
 
MakeListBoxL();
 
ActivateL();
DrawNow();
}
 
void CSearchListContainer::MakeListBoxL()
{
TInt MySetIndex(0);
 
if(iListBox)
{
MySetIndex = GetSelectedIndexL();
}
 
delete iListBox;
iListBox = NULL;
 
iListBox = new( ELeave ) CAknSingleGraphicStyleListBox();
iListBox->ConstructL(this,EAknListBoxSelectionList);
 
CArrayPtr<CGulIcon>* icons =new( ELeave ) CAknIconArray(10);
CleanupStack::PushL(icons);
 
iListBox->Model()->SetItemTextArray(GetArrayL(icons));
iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
 
CleanupStack::Pop(icons);
iListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
iListBox->CreateScrollBarFrameL( ETrue );
iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );
 
iListBox->SetRect(Rect());
 
delete iFindBox;
iFindBox = NULL;
iFindBox = CreateFindBoxL(iListBox, iListBox->Model(),
CAknSearchField::ESearch);
SizeChanged();
 
iListBox->ActivateL();
 
TInt ItemsCount = iListBox->Model()->ItemTextArray()->MdcaCount();
 
if(ItemsCount > MySetIndex && MySetIndex >= 0)
iListBox->SetCurrentItemIndex(MySetIndex);
else if(ItemsCount > 0)
iListBox->SetCurrentItemIndex(0);
 
UpdateScrollBar(iListBox);
DrawNow();
}
 
CAknSearchField* CSearchListContainer::CreateFindBoxL(CEikListBox* aListBox,
CTextListBoxModel* aModel, CAknSearchField::TSearchFieldStyle aStyle)
{
CAknSearchField* findbox = NULL;
 
if (aListBox && aModel)
{
// Gets pointer of CAknFilteredTextListBoxModel.
CAknFilteredTextListBoxModel* model =
STATIC_CAST( CAknFilteredTextListBoxModel*, aModel );
// Creates FindBox.
findbox = CAknSearchField::NewL( *this, aStyle, NULL,
KAknExListFindBoxTextLength);
CleanupStack::PushL(findbox);
// Creates CAknListBoxFilterItems class.
model->CreateFilterL( aListBox, findbox );
//Filter can get by model->Filter();
CleanupStack::Pop(findbox); // findbox
}
 
return findbox;
}
 
CDesCArray* CSearchListContainer::GetArrayL(CArrayPtr<CGulIcon>* aIcons)
{
CDesCArrayFlat* MyArray = new(ELeave)CDesCArrayFlat(10);
CleanupStack::PushL(MyArray);
 
// Append text items and icons in here...
//MyArray->AppendL(TextBuf);
//aIcons->AppendL(CGulIcon::NewL(Icon,IconMsk));
 
CleanupStack::Pop(MyArray);
return MyArray;
}
 
void CSearchListContainer::UpdateScrollBar(CEikListBox* aListBox)
{
if (aListBox)
{
TInt pos(aListBox->View()->CurrentItemIndex());
if (aListBox->ScrollBarFrame())
{
aListBox->ScrollBarFrame()->MoveVertThumbTo(pos);
}
}
}
 
void CSearchListContainer::SizeChanged()
{
if (iListBox)
{
if (iFindBox)
{
CAknColumnListBox* aknListBox = STATIC_CAST(CAknColumnListBox*,
iListBox);
AknFind::HandleFixedFindSizeChanged(this, aknListBox, iFindBox);
 
}
else
{
iListBox->SetRect(Rect()); // Sets rectangle of lstbox.
}
}
}
 
 
void CSearchListContainer::HandleResourceChange(TInt aType)
{
TRect rect;
 
if ( aType==KEikDynamicLayoutVariantSwitch )
{
AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
 
SetRect(rect);
}
 
CCoeControl::HandleResourceChange(aType);
}
 
TInt CSearchListContainer::GetSelectedIndexL(void)
{
TInt Ret(-1);
 
if(iListBox)
{
TInt CurrItemInd = iListBox->CurrentItemIndex();
 
CAknFilteredTextListBoxModel* model =
STATIC_CAST(CAknFilteredTextListBoxModel*,iListBox->Model());
 
if(model && CurrItemInd >= 0)
{
Ret = model->Filter()->FilteredItemIndex(CurrItemInd);
}
}
 
return Ret;
}
 
 
TKeyResponse CSearchListContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aEventCode)
{
TKeyResponse Ret = EKeyWasNotConsumed;
 
switch (aKeyEvent.iCode)
{
case EKeyDevice3:
break;
default:
if(iListBox)
{
if ( iFindBox )
{
TBool needRefresh( EFalse );
 
// Offers the key event to find box.
if ( AknFind::HandleFindOfferKeyEventL( aKeyEvent, aEventCode, this,
iListBox, iFindBox,
EFalse,
needRefresh ) ==
EKeyWasConsumed )
{
if ( needRefresh )
{
SizeChanged();
DrawNow();
}
 
return EKeyWasConsumed;
}
}
 
Ret = iListBox->OfferKeyEventL(aKeyEvent, aEventCode);
}
break;
}
 
 
return Ret;
}
 
void CSearchListContainer::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.Clear(Rect());
}
 
CCoeControl* CSearchListContainer::ComponentControl( TInt aIndex) const
{
if(iFindBox && aIndex)
return iFindBox;
else
return iListBox;
 
}
 
TInt CSearchListContainer::CountComponentControls() const
{
if(iListBox && iFindBox)
{
return 2;
}
else
{
return 0;
}
}

SearchListbox.h

#include <coecntrl.h>
#include <aknlists.h>
#include <EIKLBX.H>
#include <aknsfld.h>
 
const TInt KAknExListFindBoxTextLength = 20;
 
 
class CSearchListContainer : public CCoeControl
{
public:
static CSearchListContainer* NewL(void);
static CSearchListContainer* NewLC(void);
~CSearchListContainer();
public:
TInt CountComponentControls() const;
CCoeControl* ComponentControl( TInt aIndex) const;
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aEventCode);
TInt GetSelectedIndexL(void);
private:
void MakeListBoxL();
CAknSearchField* CreateFindBoxL(CEikListBox* aListBox,
CTextListBoxModel* aModel, CAknSearchField::TSearchFieldStyle aStyle);
CDesCArray* GetArrayL(CArrayPtr<CGulIcon>* aIcons);
void UpdateScrollBar(CEikListBox* aListBox);
virtual void SizeChanged();
virtual void HandleResourceChange(TInt aType);
void Draw(const TRect& aRect) const;
void ConstructL();
private:
CAknSingleGraphicStyleListBox* iListBox;
CAknSearchField* iFindBox;
};

Links

How to create a simple listbox

List box example

Querying selection with list


--

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fJ2ME45E5fE52SSE5fParserE5fwithE5fKE58mlX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxJ2ME45E20E52SSE20ParserE20withE20KE58mlE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZupdatedQDx2008E2d10E2d02X qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X