Getting the text of the currently selected list box item

Login to reply to this topic.
Thu, 2006-04-13 15:37
Joined: 2005-07-22
Forum posts: 52
Hi ,

I need to get the text of the currently selected list box item in a descruiptor. I know we can get the index of the current item selected as follows :

Code:

  iListBox->CurrentItemIndex() ;


using the CurrentItemIndex() method.I am using a CAknSingleStyleListBox , how do I get the text of the currently selected item ?

Thanks a lot Smiley

Thu, 2006-04-13 16:02
Joined: 2004-05-24
Forum posts: 982
Re: Getting the text of the currently selected list box item
You can get the array of items with the following lines

MDesCArray* textArray = listBox->Model()->ItemTextArray();
CDesCArray* listBoxItems = static_cast<CDesCArray*>(textArray);

And from here is easy Wink

pirosl

Tue, 2006-05-02 20:05
Joined: 2005-12-01
Forum posts: 13
Re: Getting the text of the currently selected list box item
What if you are working with a FindBox? The index from CurrentItemIndex() is relative to the current view, but the index of your CDesCArray is relative to the full data...

Example ListBox:
John
Bill <-- Highlighted
Tim
Tom

Code:
TInt index = iListBox->CurrentItemIndex(); // index = 2
TBuf<128> item = (*listBoxItems)[index];   // item = Bill
Correct Result.

Press "T" for FindBox in above Example ListBox:
Tim <-- Highlighted
Tom

Code:
TInt index = iListBox->CurrentItemIndex(); // index = 2
TBuf<128> item = (*listBoxItems)[index];   // item = Bill
Wrong Result. Sad
Tue, 2006-05-02 20:29
Joined: 2004-05-24
Forum posts: 982
Re: Getting the text of the currently selected list box item
It should work with Model()->ItemText(TInt aItemIndex) which sdk says it  will return the item that will be displayed at specified index.

pirosl

Tue, 2006-05-02 21:25
Joined: 2005-12-01
Forum posts: 13
Re: Getting the text of the currently selected list box item
Thanks, that works...

but... it's read-only.  For my project I acutally need to be able to modify the entry (technically not the actual display text, but its icon %d\t\%S).

Is there a way to get the "actual" CurrentItemIndex(), not relative to the current view so I can modify the current item in the list box? Or some other method of achieving a similar result...

Code:
// Replace this with something that gets the Current Item Index relative to the entire list, not the current "view"
TInt aIndex = iListBox->CurrentItemIndex();

MDesCArray* textArray = this->Model()->ItemTextArray();
CDesCArray* listBoxItems = static_cast<CDesCArray*>(textArray);

// Replace Item Icon
TBuf<128> item = (*listBoxItems)[aIndex];
_LIT(KTab, "\t");
_LIT(KNewIcon, "1"); // actually dynamically calculated elsewhere
TInt pos = item.Find(KTab);
item.Replace(0, pos, KNewIcon);

listBoxItems->Delete(aIndex);
listBoxItems->InsertL(aIndex, item);

this->DrawItem(aIndex);

I havn't found anything in the SDK for doing that, other than maybe pulling out the item text and searching for it in the full version listBoxItems... but that wouldn't handle duplicates without getting rediculously nasty by attempting to create some algorithm based on the FindBox's search text, relative index in the FindBox result, and what items were previously selected. *ugg*
Tue, 2006-05-02 22:24
Joined: 2004-05-24
Forum posts: 982
Re: Getting the text of the currently selected list box item
It's true that it returns a TPtrC but you still have a Set in TPtrC Wink. Who has the ownership of the array in your listbox? Do i understood korekt?

pirosl

Tue, 2006-05-02 22:53
Joined: 2005-12-01
Forum posts: 13
Re: Getting the text of the currently selected list box item
That's what I thought as well, but it doesn't seem to work...

Code:
this->Model()->SetOwnershipType(ELbmOwnsItemArray);

TPtrC itemBefore = this->Model()->ItemText(aIndex); // itemBefore = "0\tOld"
_LIT(KNewItem, "1\tNEW");
itemBefore.Set(KNewItem); // itemBefore = "1\tNEW"
TPtrC itemAfter = this->Model()->ItemText(aIndex); // itemAfter = "0\tOld" still
Wed, 2006-05-17 17:55
Joined: 2003-10-21
Forum posts: 40
Re: Getting the text of the currently selected list box item
Hi Inc0gn1t007 ,

did you solve this issue? I have the same problem here.

Thanks
imzadi
Wed, 2006-05-17 17:59
Joined: 2005-12-01
Forum posts: 13
Re: Getting the text of the currently selected list box item
I did, here's the code:

Code:
// Get Oringinal Item Index
TInt CMyClass::GetOriginalIndex(const TInt aVisibleIndex)
{
CAknFilteredTextListBoxModel* model = STATIC_CAST(CAknFilteredTextListBoxModel*, this->Model());
return model->Filter()->FilteredItemIndex(aVisibleIndex);
}


...


_LIT(KNewItem, "1\tNEW"); // Hardcoded example
TInt visibleIndex = this->View()->CurrentItemIndex();
TInt originalIndex = GetOriginalIndex(visibleIndex);

if (originalIndex != KErrNotFound)
{
MDesCArray* textArray = this->Model()->ItemTextArray();
CDesCArray* listBoxItems = static_cast<CDesCArray*>(textArray);
TBuf<128> oldItem = (*listBoxItems)[originalIndex];
listBoxItems->Delete(originalIndex);
listBoxItems->InsertL(originalIndex, KNewItem);
this->DrawDeferred();
}
Wed, 2006-05-17 18:01
Joined: 2003-10-21
Forum posts: 40
Re: Getting the text of the currently selected list box item
man, you are fast... Cool

Thanks
Wed, 2006-05-17 18:04
Joined: 2005-12-01
Forum posts: 13
Re: Getting the text of the currently selected list box item
no problem, gotta love that subscribed thread e-mail notification.  Grin
Tue, 2008-11-18 09:07
Joined: 2008-09-02
Forum posts: 40
Re: Getting the text of the currently selected list box item

Do you know anything about

class AknListBoxLinesTemplate

If yes Please hellp me.
I need to find out how to use it.
Thanx.

  • Login to reply to this topic.