阵列列表只有一个文件时打印三次

时间:2014-04-19 14:48:30

标签: vb.net winforms

以下函数加载ssh目录的文件并填充列表视图,其中包含供用户选择下载的文件,但由于某种原因,它将列表填充三次,将数组列表拆分为三个段(2)部分是文件名,为什么不只是在listview中添加一个元素,谢谢

 Private Sub LoadSSHFiles()      
    Dim sshController As New wcSFtp(cfb.HostName, cfb.port, cfb.username, cfb.passsword)
    Dim thisDocument As ArrayList = sshController.getDirectoryList("/")

    Dim cnt As Int16 = 0

    For Each item In thisDocument(2)

        Dim lvItem As New ListViewItem(cnt)
        lvItem.SubItems.Add(thisDocument(2).ToString())
        lvAvailableDocuments.Items.Add(lvItem)
        cnt += 1

    Next

1 个答案:

答案 0 :(得分:0)

' this line iterates thru the members of ONE element of
' the ArrayList.  If you are adding strings to the
' AL then you are iterating characters. Item is not Typed either
For Each item In thisDocument(2)

    this is creating a new array of LVI each time
    Dim lvItem As New ListViewItem(cnt)

    ' this adds the same thing to the LV 
    ' for each loop iteration
    lvItem.SubItems.Add(thisDocument(2).ToString())
    lvAvailableDocuments.Items.Add(lvItem)
    cnt += 1

Next

请参阅上面添加的注释。通常当你迭代某些东西时,你会使用索引var做一些事情;你正在迭代一件事并一遍又一遍地添加那件事。使用ArrayList,您可以更轻松地使用计数:

Dim lvi As ListViewitem

' go thru the list
For n As Integer = 0 to thisDocument.Count - 1
     lvi = new ListViewItem
     lvi.Text = thisDocument(n)
     ' or, since there are no subitems:
     ' lvi = New ListViewItem(thisDocument(n))

     lvAvailableDocuments.Items.Add(lvi)
Next n

现在,您所要做的就是正确地看到thisDocument已正确播种了您想要的文件。