Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok so my situation is basically having a ListView that updates itself (removes all items and add the same ones with different subitems etc). This is a name, and then whether they are offline or online and some other things from an online API.

Basically what happens is
>Get data from API
>Sort out data
>Add data to listview
>Check for changes since last time <--- This currently doesnt work
>If there's a change, do something (for example display a messagebox displaying Listviews(i).item.text, e.g. the name)
>Repeat every x seconds

So what I'm trying to do is to check if for example someone went from online to offline.

I first tried an approach where I simply stored the listview in an array like array(i) = name|online, then refresh the listview, and do the same in a new array, then compare each string in the array to find the difference, and then easily fetch the name from it. This does not work because in my listview the Item is the status, and the first subitem(1) is the name, so it's sorted by name and the order after each refresh is randomised, so therefore it will mix up and not work properly. The reason it's like this is for simplicity, and having status to the leftmost in the listview is absolutely preferred.

I tried another approach where I made a temporary listview in the code and transferred each item from listview1 to it, sorted it, and put it into an array. Then did the same as above, but also with this temporary listview at the second array before I compared them. This way it should be the same order. Keep in mind that the amount of items in the listview is not changed during this whole comparison process. This, for some reason, does not work. I have no idea why.

Does anyone have an idea of what to try?
By this point my code is pretty messy due to trying different stuff, but here's basically the whole sub where I store data to the array, get new data, and store the new data to a new array and compare it:
(Note that the webclient part is working just fine, so you can actually ignore it. What it does is clear ListView1, download data, and add the new data to ListView1. The problem here is that it sorts by status (Online/Offline) so the order is randomized each time).
VB
Dim NewList As ListView = New ListView
        NewList.Items.Clear()
        For i = 0 To ListView1.Items.Count - 1
            Dim itm As ListViewItem = New ListViewItem(ListView1.Items(i).SubItems(1).Text)
            Try
                itm.SubItems.Add(ListView1.Items(i).Text)
                itm.SubItems.Add(ListView1.Items(i).SubItems(2).Text)
                itm.SubItems.Add(ListView1.Items(i).SubItems(3).Text)
                itm.SubItems.Add(ListView1.Items(i).SubItems(4).Text)
                NewList.Items.Add(itm)
            Catch : End Try
        Next
        NewList.Sorting = SortOrder.Ascending
        NewList.Sort()

            'Check for changes
        Dim oldusers(NewList.Items.Count - 1) As String
        For i = 0 To NewList.Items.Count - 1
            oldusers(i) = NewList.Items(i).Text + "|" + NewList.Items(i).SubItems(1).Text
        Next




            'Request (this code is pretty unrelated to my problem, it clears the ListView1 for items and then adds items from an online source. the amount is the same as before. The name does not change (subitem(1)).)
            Try
                Using wc As New WebClient
                    Dim data As String = wc.DownloadString(xxx) 'This will return something like "assaasd|asdasdasd|asdadasd|asdasdasd|asdasdsad¤"        repeatedly some times depending on user input
                    Dim allusers() As String = data.Split("¤")
                    ListView1.Items.Clear()
                    For i = 0 To allusers.Length - 2
                        Dim myArray() As String = allusers(i).Split("¨")
                        If myArray(1) = "Offline" Then
                            Dim tmpItem As New ListViewItem("Offline")
                            tmpItem.SubItems.Add(myArray(0))
                            ListView1.Items.Add(tmpItem)
                        Else
                            Try
                                
                                Dim tmpItem As New ListViewItem("Online") 'Status
                                tmpItem.SubItems.Add(myArray(1)) 'Name
                                tmpItem.SubItems.Add(myArray(2))
                                tmpItem.SubItems.Add(myArray(3))
                                Dim s() As String = myArray(4).Split("T")
                                tmpItem.SubItems.Add(s(1).Replace("Z", "").Replace("T", ""))
                                ListView1.Items.Add(tmpItem)
                            Catch ex As Exception

                            End Try
                        End If
                    Next
                    sessionCount += 1
                    Label3.Text = "Session count: " & CStr(sessionCount)
                    wc.Dispose()
                End Using
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try

        NewList = New ListView
        NewList.Items.Clear()
        For i = 0 To ListView1.Items.Count - 1
            Dim itm As ListViewItem = New ListViewItem(ListView1.Items(i).SubItems(1).Text)
            Try
                itm.SubItems.Add(ListView1.Items(i).Text)
                itm.SubItems.Add(ListView1.Items(i).SubItems(2).Text)
                itm.SubItems.Add(ListView1.Items(i).SubItems(3).Text)
                itm.SubItems.Add(ListView1.Items(i).SubItems(4).Text)
                NewList.Items.Add(itm)
            Catch : End Try
        Next
        NewList.Sorting = SortOrder.Ascending
        NewList.Sort()




        'Check for changes
        Dim newusers(NewList.Items.Count - 1) As String
        For i = 0 To newusers.Length - 1
            newusers(i) = NewList.Items(i).Text + "|" + NewList.Items(i).SubItems(1).Text
            Try
                If newusers(i) = oldusers(i) Then
                    'NO CHANGE!

                Else
                    'CHANGE!
                    Dim userer() As String = newusers(i).Split("|") 'name|Offline <ex
                    Notify(userer(0), userer(1))
                End If
            Catch : End Try
        Next




The ListView1 looks like this

Item |SubItem(1)|SubItem(2)|SubItem(3)|SubItem(4)
Status|Name |x|x|x

Whatever is in the x's is irrelevant.

Thanks!
Posted
Updated 7-Jul-14 1:10am
v2

you should continue trying your first approach with only a few modifications:

First: You will need two arrays.

It isn't your array which relies on the list, but the list must rely on the array, therefore just add the items to the list through a foreach on the array.

At the start point, both arrays must have equal items. Although one should be named "mod" or something that assembles you that it will be the one that is going to be modified first.

Second: If you have trouble to customize your array, use a new class to provide a template for it: It will be easier to add to your list of your array items have the exact fields of the list items.

Third: now you will only modify the ARRAYS, not the list items directly. When you are done, just clear listview items and add everything back again.

Forth: Everytime there will be modifications, update only one array (the "mod" one) and use it to compare with the other one. That will need some algorithm.

Fifth: When you are done checking for the modifications, clone it to the unchanged array again and keep repeating the process.

by the way, I'm using arrays, just because you used it before me. But personally, I would go for List.
 
Share this answer
 
I don't know why I did not think of using a list instead. I swapped to a list, and thus it was much easier to debug aswell. I think my real mistake was that I messed up the arrays so it compared wrong parts or something.
Nevertheless, I got it working now. Thanks!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900