从现在开始,在刷新后查找ListView中的差异[VB.NET] [英] Finding differences in a ListView from now and after it refreshes [VB.NET]

查看:109
本文介绍了从现在开始,在刷新后查找ListView中的差异[VB.NET]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我的情况基本上是一个自我更新的ListView(删除所有项目并添加具有不同子项目的相同项目等)。这是一个名称,然后是在线还是在线以及其他来自在线API的东西。



基本上会发生什么是

>从API获取数据

>整理数据

>将数据添加到列表视图

>检查自上次以来的变化< ; ---这当前不起作用

>如果有变化,做一些事情(例如显示一个显示Listviews(i).item.text的消息框,例如名称)

>每隔x秒重复一次



所以我要做的就是检查一下是否有人从在线到离线。



我首先尝试了一种方法,我只是将listview存储在数组中,如array(i)= name | online,然后刷新listview,并在新数组中执行相同操作,然后比较数组中的每个字符串以找到差异,然后轻松地从中获取名称。这不起作用,因为在我的listview中,Item是状态,第一个子项(1)是名称,所以它按名称排序,每次刷新后的顺序是随机的,因此它会混淆而不能正常工作。这样的原因是为了简单起见,并且在列表视图中最左边的状态是绝对首选。



我尝试了另一种方法,我在其中制作了一个临时列表视图代码并将listview1中的每个项目传输给它,对其进行排序,并将其放入数组中。然后做了同样的事情,但在我比较它们之前,还在第二个数组使用了这个临时列表视图。这样它应该是相同的顺序。请记住,在整个比较过程中,列表视图中的项目数量不会更改。由于某种原因,这不起作用。我不知道为什么。



有没有人知道该尝试什么?

到此为止由于尝试了不同的东西,我的代码非常混乱,但这里基本上是整个sub,我将数据存储到数组,获取新数据,并将新数据存储到新数组并进行比较:

请注意,webclient部分工作正常,因此您实际上可以忽略它。它的作用是清除ListView1,下载数据,并将新数据添加到ListView1。这里的问题是它排序按状态(在线/离线),所以订单每次都是随机的。)

  Dim  NewList 作为 ListView =  ListView 
NewList.Items.Clear()
对于 i = 0 ListView1.Items.Count - < span class =code-digit> 1
Dim itm As ListViewItem = ListViewItem(ListView1 .Items(i).SubItems( 1 )。文本)
尝试
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 结束 尝试
Next
NewList.Sorting = SortOrder.Ascending
NewList.Sort()

' 检查更改
Dim oldusers(NewList.Items。计数 - 1 作为 字符串
对于 i = 0 NewList .Items.Count - 1
oldusers(i)= NewList.Items(i).Text + | + NewList.Items(i).SubItems( 1 )。文字
下一步




' 请求(此代码与我的问题非常无关,它清除项目的ListView1,然后从在线源添加项目。金额与以前相同。名称不会更改(subitem(1))。)
尝试
使用 wc 作为 WebClient
Dim 数据作为 字符串 = wc.DownloadString(xxx)' 根据用户输入,这将多次返回类似assaasd | asdasdasd | asdadasd | asdasdasd |asdasdsad¤的内容
Dim allusers() As String = data.Split( ¤
ListView1.Items.Clear()
对于 i = 0 allusers.Length - 2
Dim myArray() As String = allusers(i)。拆分( ¨
如果 myArray( 1 )= 离线 然后
Dim tmpItem As ListViewItem( 离线
tmpItem.SubItems.Add(myArray( 0 ))
ListView1.Items.Add(tmpItem)
否则
尝试

Dim tmpItem As ListViewItem( 在线' 状态
tmpItem.SubItems.Add(myArray( 1 ))' 名称
tmpItem.SubItems.Add(myArray( 2 ))
tmpItem.SubItems.Add( myArray( 3 ))
Dim s()作为 String = myArray( 4 )。分割( T
tmpItem.SubItems.Add(s( 1 )。替换( Z )。替换( T ))
ListView1.Items.Add(tmpItem)
Catch ex As 例外

结束 尝试
结束 如果
下一步
sessionCount + = 1
Label3.Text = 会话计数:& CStr (sessionCount)
wc.Dispose()
结束 使用
Catch ex 作为例外
MsgBox(ex.ToString())
结束 尝试

NewList = ListView
NewList.Items.Clear()
对于 i = 0 ListView1.Items.Count - 1
Dim itm As ListViewItem = ListViewItem(ListView1.Items(i).SubItems( 1 )。Text)
尝试
itm.SubItems.Add(ListView1.Items(i).Text)
itm.SubItems.Add(ListView1.Items(i).SubItems( 2 )。文本)
itm.SubItems.Add(ListView1.Items(i).SubItems( 3 ) .Text)
itm.SubItems.Add(ListView1.Items(i).SubItems( 4 )。Text)
NewList.Items.Add (itm)
Catch 结束 尝试
下一步
NewList.Sorting = SortOrder.Ascending
NewList.Sort()




' 检查更改
Dim newusers(NewList.Items.Count - 1 As 字符串
对于 i = 0 newusers.Length - 1
newusers(i)= NewList.Items(i).Text + | + NewList.Items(i).SubItems( 1 )。文字
尝试
如果 newusers(i)= oldusers(i)然后
' NO CHANGE !

否则
' 更改!
Dim userer()作为 字符串 = newusers(i).Split( |' name | Offline< ex
通知(userer( 0 ),userer ( 1 ))
结束 如果
Catch 结束 尝试
下一页









ListView1看起来像这样



Item | SubItem(1)| SubItem(2)| SubItem(3)| SubItem( 4)

状态|名称| x | x | x



x中的任何内容都无关紧要。



谢谢!

解决方案

您应该继续尝试第一种方法,只需进行少量修改s:



第一:你需要两个阵列。



这不是你的阵列依赖在列表中,但列表必须依赖于数组,因此只需通过阵列上的foreach将项添加到列表中。



在起始点,两个数组必须具有相同的项目。虽然应该将一个命名为mod或者组装你的东西,它将是首先被修改的那个。



第二:如果您无法自定义阵列,请使用新类为其提供模板:将更容易添加到阵列列表中项目具有列表项目的确切字段。



第三:现在您只会修改ARRAYS,而不是直接修改列表项目。当你完成后,只需清除listview项目并再次添加所有内容。



Forth:每次都会有修改,只更新一个数组(mod一个)并用它来比较另一个。这将需要一些算法。



第五:当你完成检查修改后,再次将其克隆到未更改的数组并继续重复该过程。
顺便说一句,


,我正在使用数组,只是因为你在我面前使用它。但就个人而言,我会选择List。


我不知道为什么我没有考虑使用列表。我交换到一个列表,因此调试也容易得多。我认为我真正的错误是我弄乱了数组,所以它比较了错误的部分或者其他东西。

尽管如此,我现在已经开始工作了。谢谢!

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()

            '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!

解决方案

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.


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!


这篇关于从现在开始,在刷新后查找ListView中的差异[VB.NET]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆