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).
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()
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
Try
Using wc As New WebClient
Dim data As String = wc.DownloadString(xxx)
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")
tmpItem.SubItems.Add(myArray(1))
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()
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
Else
Dim userer() As String = newusers(i).Split("|")
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!