List Box Adding Data Dynamically

Login to reply to this topic.
Tue, 2007-10-16 13:45
Joined: 2007-09-19
Forum posts: 71

Hi
The contents of my listbox are going to added dynamically..
so initially when i give the model class the ownership of the array the problem is i dont know the size f the array.. as it will grow gradually...

Initially when my array size used to be fixed that time i used to allocate memory for
CDesC16Array type and format the strings accordingly before the assigning the itemtext array...

iListBox->Model()->SetItemTextArray(itemList); //MDesCArray * type
iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);

Now since my array size is gradually going to increase how should i do the memory allocation and when should i assign the ItemTextArray to Model ?

Thanks,
Sohil.


Wed, 2007-10-17 18:09
Joined: 2003-12-05
Forum posts: 622
Re: List Box Adding Data Dynamically

Well check out the MDesCArray documentation. It has two methods, the first one returns the count of elements, the second the nth element. So the listbox will ask the number of elements from the model. Voilá!

Thu, 2007-10-18 07:16
Joined: 2006-09-15
Forum posts: 26
Re: List Box Adding Data Dynamically

You can take a array to store all the list items like

CDesCArray* targetArray = static_cast( aTargetArray );

targetArray->Reset();
targetArray->Compress();

And store the text in HBufC and append it in array.

item->Des().Format( _L("%D\t%S\t"),( *name)[a]+1, name );
targetArray->AppendL( *item );

And draw the listbox using this array.

i think it ll help u.


Ruchi.

Thu, 2007-10-18 07:33
Joined: 2007-09-19
Forum posts: 71
Re: List Box Adding Data Dynamically

hi,
Yeah the question i have is

can i reallocated memory for my array without destroying the current contents ?

Thanks

Thu, 2007-10-18 07:53
Joined: 2006-04-19
Forum posts: 134
Re: List Box Adding Data Dynamically

Hi

check this :

From SDK :

"In a CArrayFixFlat or a CArrayFixSeg array, the number of elements in an array can be changed, either decreased or increased, using the ResizeL() function.

CArrayFixFlat* fixflat;
fixflat = new (ELeave) CArrayFixFlat(3);
...
fixflat->ResizeL(1); "

Hope this will help you .

Regards
Isha


"To the question of your life , you are the only Answer. To the problems of your life,you are the only Solution".

Thu, 2007-10-18 08:11
Joined: 2007-09-19
Forum posts: 71
Re: List Box Adding Data Dynamically

Thanks Isha..
i will try that out...

Thu, 2007-10-18 09:57
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 1918
Re: List Box Adding Data Dynamically

Just append items to your array (itemList->AppendL(newItem)) but you will have to use the base class of itemList, not the MDesCArray.
Then you call iListbox->HandleItemAdditionL() and that's it.

I am not very sure of the relevance to use Resize() or any HBufC to add items to your array.


Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant

Thu, 2007-10-18 10:05
Joined: 2007-09-19
Forum posts: 71
Re: List Box Adding Data Dynamically

hi,
I would like to clear my doubt..

My item list which is
CDesC16Array *itemList;

initiailly when i add few elements i allocate memory for the same...

itemList = new (ELeave) CDesC16ArrayFlat(aCnt);
formating the string...
and calling HandleItemAddition

once done that for every new entry i can directly use itemList->AppendL(); .. no need to resize the itemList ?
and cal handleitemaddition ...

Thanks,

Thu, 2007-10-18 10:14
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 1918
Re: List Box Adding Data Dynamically

Yes, you just need to call AppendL to add item to your array. HandleItemAddition is only required to tell the listbox to refresh itself. And you don't need to reallocate the array or anything, this will be handled by the system.

ACnt is the granularity of the array, not the max number of element in it.


Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant

Thu, 2007-10-18 11:10
Joined: 2007-09-19
Forum posts: 71
Re: List Box Adding Data Dynamically

Hi Eric ,
Appreciate you help..
it seems i have completely misunderstood...while

itemList = new (ELeave) CDesC16ArrayFlat(aCnt);

i thought aCnt is the length of the array..

can you please differentiate between granularity and max number of elemnts ?

Thanks
Sohil

Thu, 2007-10-18 22:53
Joined: 2003-12-05
Forum posts: 622
Re: List Box Adding Data Dynamically

The array will take care of extending itself automagically. The granularity means the number of new empty elements the array will add when it runs out of empty elements. So if you have an array with capacity to hold 8 elements and granularity is also 8, then you add new ones (using AppendL) one by one and when the 8th element is added, the array will extend itself to capacity of 16 (8+8). Now it holds 8 elements and has room for additional 8 before it needs to further extend itself.

All this is explained in the SDK docs, please read.

  • Login to reply to this topic.