[Gambas-user] A random sort of listview

Simonart Dominique simonart.dominique at ...11...
Sun Apr 12 12:08:35 CEST 2009


Hi again :)

May be you did not see the last sentence of my answer wich 
is written in 3 places? Your code is just fine and I don't 
have any problem with the ListViews. Of course, I had to 
initialize the ListView1 with my own data like this:

FOR i = 0 to 21
    ListView1.Add(Str(i), "number " & Str(i))
NEXT
After that, I run your code and all is fine!
If you have problem, this is therefore with the content of 
your Listview1. I think it is not correct to have identical 
keys in a ListView because if I attempt to do it the program 
stop immediatly. I even don't understand how you could get 
this situation! Here is what I tried:

FOR i = 0 to 21
    SELECT CASE i
    CASE 0
       ListView1.Add("5", "number " & Str(i))
    CASE 1
       ListView1.Add("13", "number " & Str(i))
    CASE ELSE
       ListView1.Add(Str(i), "number " & Str(i))
    END SELECT
NEXT
this code stop with i=5 and say that the key is already used!

Could you make a try with unique keys only?
Plus, could you run it without TRY?

Hope we could identify the error
Dominique Simonart

jbskaggs a écrit :
> Hi yourself!  Here is the problem in the code:
> 
> FOR i = 0 TO ListView1.Count - 1 ' write items in listview2
> TRY PRINT myArray[i]
> listview1.MoveTo(myArray[i])
> TRY c = listview1.Item.Key
> TRY PRINT myArray[i], "listview1.item.key ", c, " counter i: ", i, " total
> count: ", listview1.Count
> TRY listview2.add(c, listview1.item.text) 
> NEXT 
> 
> When the counter i reaches Listview1.Count -1  it gives an error and does
> not write the last record!   I have compensated by writing an extra  dummy
> record to the data file- but how do I code this properly to write the whole
> list?  I have tried several things and they just give different errors.
> 
> Thanks
> 
> JB
> 
> Simonart Dominique wrote:
>> Hi,
>>
>> jbskaggs a écrit :
>>> Okay I thought this was worked out but its still not working- I always
>>> end up
>>> losing the last two list items.
>>>
>>> Here is the console output from my print statements:
>>> This first list is printing i, a from this command SWAP myArray[i],
>>> myArray[a]
>>>
>>> 21      21
>>> 20      13
>>> 19      0
>>> 18      13
>>> 17      11
>>> 16      4
>>> 15      9
>>> 14      12
>>> 13      4
>>> 12      2
>>> 11      6
>>> 10      2
>>> 9       4
>>> 8       0
>>> 7       5
>>> 6       1
>>> 5       3
>>> 4       3
>>> 3       0
>>> 2       0
>>> 1       0
>>> 0       0
>>>
>> Here, you listed i and a but the interesting values are i 
>> and myArray[i] instead
>>
>>> This list shows where listview1 is being copied into listview2:
>>>
>>> listview1.item.key      21       counter i:     21       total count:  
>>> 22
>>> listview1.item.key      13       counter i:     20       total count:  
>>> 22
>>> listview1.item.key      13       counter i:     19       total count:  
>>> 22
>>> listview1.item.key      20       counter i:     18       total count:  
>>> 22
>>> listview1.item.key      11       counter i:     17       total count:  
>>> 22
>>> listview1.item.key      4        counter i:     16       total count:  
>>> 22
>>> listview1.item.key      9        counter i:     15       total count:  
>>> 22
>>> listview1.item.key      12       counter i:     14       total count:  
>>> 22
>>> listview1.item.key      16       counter i:     13       total count:  
>>> 22
>>> listview1.item.key      2        counter i:     12       total count:  
>>> 22
>>> listview1.item.key      6        counter i:     11       total count:  
>>> 22
>>> listview1.item.key      14       counter i:     10       total count:  
>>> 22
>>> listview1.item.key      18       counter i:     9        total count:  
>>> 22
>>> listview1.item.key      19       counter i:     8        total count:  
>>> 22
>>> listview1.item.key      5        counter i:     7        total count:  
>>> 22
>>> listview1.item.key      5        counter i:     6        total count:  
>>> 22
>>> listview1.item.key      3        counter i:     5        total count:  
>>> 22
>>> listview1.item.key      7        counter i:     4        total count:  
>>> 22
>>> listview1.item.key      8        counter i:     3        total count:  
>>> 22
>>> listview1.item.key      15       counter i:     2        total count:  
>>> 22
>>> listview1.item.key      10       counter i:     1        total count:  
>>> 22
>>> listview1.item.key      17       counter i:     0        total count:  
>>> 22
>>>
>>> here is a screen shot of the two lists: listview1 is on the left, you
>>> will
>>> notice that the last two items are not on the random sorted list on the
>>> right.
>>>
>> First, is it correct that you have twice the keys 13 and 5 
>> in ListView1 and that keys 0 and 1 are missing?
>> I tried to reproduce exactly your keys sequence but as soon 
>> as I want to add an identical key, I get a message and the 
>> programme stop immediatly.
>>
>>> http://www.nabble.com/file/p23005690/ScreenshotBug.png 
>>>
>>>
>>> Here is my code:
>>>
>>> PUBLIC SUB button4_click()
>>> DIM myArray AS Integer[ListView1.count]
>>> DIM a AS Integer
>>> DIM i AS Integer
>>> DIM c AS String
>>>
>>> FOR i = 0 TO ListView1.count - 1 STEP 1
>>>  myArray[i] = i 
>>> NEXT 
>>>
>>> FOR i = ListView1.count - 1 TO 0 STEP -1
>>> a = Int(Rnd(i + 1))
>>> SWAP myArray[i], myArray[a]
>>> PRINT i, a
>>> NEXT 
>>>
>>> FOR i = ListView1.count - 1 TO 0 STEP -1 ' write items in listview2
>>> -<<<<< I
>>> have also tried running this counting up as well but it changed nothing.
>>>
>>> listview1.MoveTo(myArray[i])
>>> TRY c = listview1.Item.Key
>>> TRY PRINT "listview1.item.key ", c, " counter i: ", i, " total count: ",
>>> listview1.Count
>>> TRY listview2.add(c, listview1.item.text) 
>>> NEXT 
>>> 'fGlobal.show
>>> 'FRolldice.Show
>>>
>>> 'ME.Hide
>>>
>>> END
>>>
>>>
>>> Sincerly JB SKaggs
>>>
>> I run your code and it works quite fine!
>> I think your missing items come from the duplicated keys
>>
>> Dominique Simonart
>>
>>
>> ------------------------------------------------------------------------------
>> This SF.net email is sponsored by:
>> High Quality Requirements in a Collaborative Environment.
>> Download a free trial of Rational Requirements Composer Now!
>> http://p.sf.net/sfu/www-ibm-com
>> _______________________________________________
>> Gambas-user mailing list
>> Gambas-user at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/gambas-user
>>
>>
> 






More information about the User mailing list