向 Listview 添加大量项目 [英] Adding huge number of items to a Listview
问题描述
我有一个列表视图,用作简单搜索应用程序的索引.索引中的每一项都是一个词,单击该项会将该项添加到搜索文本框中.用户可以先单击她/他喜欢的任何单词并将它们放到搜索文本框中,然后单击搜索,在文档中进行搜索.问题是向 ListView 添加大约 1000 多个项目需要大量运行时间!我设计了一个进度条并添加了一个计时器,该计时器在表单加载后立即开始向列表视图添加项目.这为应用程序提供了响应能力,但效率仍然很低.我怀疑当文档库增长到足够多时,索引中可能会有大约 100,000 个单词,所以我需要一种更有效的方法来做到这一点.也许我需要将 ListView 组件更改为其他内容.这是将项目添加到列表视图的计时器中的代码:
I have a listview, which is used as an index to a simple search application. Each item of the index is a word, and clicking on the item will add that item to the search textbox. The user can first click on any of the words she/he prefers to and them to the search textbox and then click search, to search in the documents. The problem is that adding more than around 1000 items to a ListView takes a lot of runtime! I have designed a progressbar and added a timer which starts adding items to the listview as soon as the form loads. This gives responsiveness to the application, but still the efficiency is very low. I suspect there might be around 100,000 words in the index when the document base grows enough so I need a more efficient way to do this. Maybe I need to change the ListView component to something else. This is the code in the timer to add the items to the listview:
if (!listViewDone)
{
int pos = 0;
ListView listView1 = Search.getInstance().getListView();
listView1.BeginUpdate();
for (pos = listViewPos; pos < termf.Count && pos < listViewPos + listViewChunk; ++pos)
{
TermFreq t = termf[pos];
listView1.Items.Add(new ListViewItem(new String[] { t.term }));
progressBar1.Value = pos;
}
listView1.EndUpdate();
listViewPos = pos;
if (pos == termf.Count)
{
listViewDone = true;
termf = null;
timer1.Enabled = false;
Visible = false;
}
}
推荐答案
正如 hmemcpy 所提到的,VirtualMode
将大大加快速度.我不确定该项目的商业性质,但我使用了优秀的开源 ObjectListView 包括一个 FastObjectListView
变体.
As hmemcpy mentioned, the VirtualMode
will speed things up considerably. I'm not sure about the commercial nature of the project but I have used the excellent open source ObjectListView which includes a FastObjectListView
variant.
这基本上是一个扩展的虚拟 ListView,速度非常快,而且作为额外的好处,使用起来要好得多.文档声称它可以在不到 0.1 秒的时间内构建一个包含 10,000 个对象的列表",虽然我无法保证每次使用它时我都没有注意到任何严重的滞后.不过,许可可能对您来说是个问题.
This is basically an extended virtual ListView that is extremely fast and as an added bonus is much nicer to work with. The documentation claims it "can build a list of 10,000 objects in less than 0.1 seconds" and while I can't vouch for that whenever I have used it I've never noticed any serious lag. The licensing could be an issue for you though.
这篇关于向 Listview 添加大量项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!