如何检索listview中的所有不同项目 [英] How do I retrieve all different items in listview

查看:56
本文介绍了如何检索listview中的所有不同项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何在ListView中获取所有不同的项目(不是子项目)。

例如:

Item1 < br $>
Item1

Item2

Item2

Item3

我如何计算每个不同的项目?输出应该如下:

Item1:2

Item2:2

Item3:2

谢谢,

iProgramIt

Hi guys,
How do I get all different items (not subitems) in a ListView.
For example:
Item1
Item1
Item2
Item2
Item3
And how do I get the count of each different item? The output should look like:
Item1 : 2
Item2 : 2
Item3 : 2
Thanks,
iProgramIt

推荐答案

如果我正确理解您的问题,这不是用户界面问题,而是与数据有关。



我认为你拥有的数据来自某些收集或数据集。如果您需要显示不同的项目以及数据中每个项目的数量,我将构建一个LINQ查询,以根据项目名称对数据进行分组,并计算每个组中的项目数量。通过这种方式,您可以获得一个带有计数的新项目列表,您可以将其用作列表视图的数据源。



查看 Group By Clause(Visual Basic) [ ^ ],这是一个很好的代码示例,非常接近您的需要。
If I understand your problem correctly, this is not an user interface problem but related to the data.

I take it the data you have is in some kond of collection or data set. If you need to show distinct items and the amount of each item in your data I'd build a LINQ query to group the data based on the item name and count the amount of items in each group. This way you would get a new list of items with count which you could use as a data source for the listview.

Have a look at Group By Clause (Visual Basic)[^], there's a nice code example, very close to your need.


First总之,为了有效地解决这些问题,您可以使用基于哈希码的数据结构,在.NET BCL中用于实现某些集合,例如关联数组。这些算法为搜索已添加项目提供O(1)的时间复杂度。请参阅:

http://en.wikipedia.org/wiki/Associative_array [< a href =http://en.wikipedia.org/wiki/Associative_arraytarget =_ blanktitle =New Window> ^ ],

https://en.wikipedia.org/wiki/Big_O_notation [ ^ ],

http ://en.wikipedia.org/wiki/Time_complexity [ ^ ]。



如果您不必为每个项目找到计数,您将使用 HashSet ;收集集合实例中的项目,在添加之前检查项目是否已在集合中。我认为这应该是清楚的。请参阅:

https:// msdn .microsoft.com / zh-CN / library / bb359438%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/bb356440(v = vs.110)的.aspx [ ^ ]。



如果您还需要计算出现次数,则需要基于键值对的类似集合,例如 Dictionary 。这是你的算法:键应该代表你的项目(不一定是字符串,你定义等价和哈希值的任何对象),值应该代表计数。每次获得新项目时,首先要检查它是否已经在集合中。但是不要使用 ContainsKey ,使用 TryGetValue ,这样你就可以检查项目的当前状态并获取项目它在收藏中。如果已添加项目,则增加字典中与其对应的值部分,计数。如果没有,请添加一个计数为1.这就是全部。



请参阅:

https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29 .aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.110).aspx [ ^ ](无需使用它),

https://msdn.microsoft.com/en-us/library/bb347013(v = vs.110).aspx [ ^ ](用于检查)。



-SA
First of all, to solve such problems efficiently, you can use data structures based on hash code and buckets, used in .NET BCL for implementation of some collections such as associative arrays. These algorithms offer the time complexity of O(1) for search of "already added item". Please see:
http://en.wikipedia.org/wiki/Associative_array[^],
https://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

If you did not have to find counts for each item, you would use HashSet; collect items in an instance of a set, checking up if the item is already in a set before adding. I think this should be clear. Please see:
https://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/bb356440(v=vs.110).aspx[^].

If you also need to calculate counts of occurrences, you need a similar collection based on key-value pair, such as Dictionary. Here is your algorithm: keys should represent your items (not necessarily strings, any objects for which you define equivalence and hash value), values should represent counts. Each time you get new item, you first check if it is already in the collection. But don't use ContainsKey, use TryGetValue, so you can check the present of the item and get the item if it is in the collection. If the item already added, increment the value part corresponding to it in the dictionary, its count. If not, add one with the count of 1. That's all.

Please see:
https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.110).aspx[^] (no need to use it),
https://msdn.microsoft.com/en-us/library/bb347013(v=vs.110).aspx[^] (use this for the check).

—SA


我只需枚举一系列包含大量选项的值,枚举ListView项,创建字典并存储键和值。然后将键和值添加到ListView以获取计数:

I simply had to enumerate through an array of values which had a bunch of options in them, enumerate the ListView items, create a dictionary and store keys and values in that. Then the key and value is added to the ListView to get the count:
Private XItems As String() = {"Windows 2000", "Windows 2000 (XBOX)", "Windows XP", "Windows Vista", "Windows 7", _
                                 "Windows 8", "Windows 8.1", "Windows 10", "OS X", "Android", "iOS", "Linux", "Ubuntu", "Other..."}
    Private DICT As New Dictionary(Of String, Integer)
    Private Sub LVLoad()
        DICT.Clear()
        ListView2.Items.Clear()
        For Each ITM As String In XItems
            DICT.Add(ITM, 0)
        Next
        For Each ITM As String In XItems
            For Each XITM As ListViewItem In ListView1.Items
                If XITM.Text = ITM Then
                    DICT(ITM) += 1
                End If
            Next
            ListView2.Items.Add(ITM).SubItems.Add(DICT(ITM))
        Next
    End Sub


这篇关于如何检索listview中的所有不同项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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