ListView in virtualmode
-
Hi This is not a problem, I'm just trying to make myself clear how ListView works in virtualmode :) So, I have ListView1 in virtualmode. My ListView1 gets virtualitems from array, "baseArray() as ListViewItem": Private Sub ListView1_RetrieveVirtualItem(..) Dim x As Integer x = e.ItemIndex e.Item = baseArray(x) End Sub This works fine and is really fast. The thing that I don't understand: Changing the text of displayed ListViewItem or it's subitem affects also the corresponding item in baseArray. This: ListView1.Items(iSomeIndex).SubItems(0).Text = "sNewText" Changes also the corresponding item in baseArray. Why this happens? It seems that: ListView1.Items(iSomeIndex) is actually (I mean physically, sorry for the expression) baseArray(iSomeIndex) I earlier thought, that virtualmode copies shown items from baseArray. But it seems that virtualmode does not work quite that way. Could some of you explain what happens when virtualmode gets items from baseArray. Thanks.
-
Hi This is not a problem, I'm just trying to make myself clear how ListView works in virtualmode :) So, I have ListView1 in virtualmode. My ListView1 gets virtualitems from array, "baseArray() as ListViewItem": Private Sub ListView1_RetrieveVirtualItem(..) Dim x As Integer x = e.ItemIndex e.Item = baseArray(x) End Sub This works fine and is really fast. The thing that I don't understand: Changing the text of displayed ListViewItem or it's subitem affects also the corresponding item in baseArray. This: ListView1.Items(iSomeIndex).SubItems(0).Text = "sNewText" Changes also the corresponding item in baseArray. Why this happens? It seems that: ListView1.Items(iSomeIndex) is actually (I mean physically, sorry for the expression) baseArray(iSomeIndex) I earlier thought, that virtualmode copies shown items from baseArray. But it seems that virtualmode does not work quite that way. Could some of you explain what happens when virtualmode gets items from baseArray. Thanks.
A ListView shows objects, whether it is used in virtual mode or not. If you set the item at index X to be baseArray(X), then you simply copy the object reference from inside your baseArray array towards the items array inside the ListView, so whatever you do to your ListView item is changing your object in the baseArray, as they are one and the same object. BTW: it does not make sense to have an array that "shadows" your items in a ListView, when you decided you want virtual mode; virtual mode is meant to overcome memory or speed limitations, that would arise when you would put a huge number of objects on a ListView, which in non-virtual mode would mean they have to reside in memory. So with virtual mode, you could create kind of a caching scheme, where only the currently visible objects need to be in memory. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
A ListView shows objects, whether it is used in virtual mode or not. If you set the item at index X to be baseArray(X), then you simply copy the object reference from inside your baseArray array towards the items array inside the ListView, so whatever you do to your ListView item is changing your object in the baseArray, as they are one and the same object. BTW: it does not make sense to have an array that "shadows" your items in a ListView, when you decided you want virtual mode; virtual mode is meant to overcome memory or speed limitations, that would arise when you would put a huge number of objects on a ListView, which in non-virtual mode would mean they have to reside in memory. So with virtual mode, you could create kind of a caching scheme, where only the currently visible objects need to be in memory. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
Thanks First part of Your answer was clarifying. But this is not clear yet: "it does not make sense to have an array that "shadows" your items in a ListView Do you mean that it's not clever to use an array of ListViewItems, because the array itself consumes memory or speed? But how could it be done then? Data needs to be provided somehow for the ListView. Data shown in ListView is stored in Database. During startup data is collected into this baseArray. Do you mean that data could be taken from the database into cache in runtime? jtpaa
-
Thanks First part of Your answer was clarifying. But this is not clear yet: "it does not make sense to have an array that "shadows" your items in a ListView Do you mean that it's not clever to use an array of ListViewItems, because the array itself consumes memory or speed? But how could it be done then? Data needs to be provided somehow for the ListView. Data shown in ListView is stored in Database. During startup data is collected into this baseArray. Do you mean that data could be taken from the database into cache in runtime? jtpaa
jtpaa wrote:
During startup data is collected into this baseArray
That means all data is in memory, and virtual mode will not bring anything.
jtpaa wrote:
Do you mean that data could be taken from the database into cache in runtime?
Yes. A very simple implementation would be to have RetrieveVirtualItem to go out and fetch one item from the database and feed it to the ListView. That way, you would not be holding any data in memory yourself, and the ListView would hold whatever it needs, and no more. I am not saying this is the best approach, as it would cause a lot of small Database operations. There typically is an optimum somewhere in between; if you are willing to spend the effort, you could: - create a cache, keeping a limited number of items, using WeakReference to make them garbage collectable; - inside RetrieveVirtualItem, first look for the item in the cache; if found, pass it on. If not, read a number of items from the database, add them to the cache, and pass on the wanted one. That is why it is called virtual: the ListView will act as if it has all data, but in reality it only has part of the data at any one point in time, sufficient to show the visible parts. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
jtpaa wrote:
During startup data is collected into this baseArray
That means all data is in memory, and virtual mode will not bring anything.
jtpaa wrote:
Do you mean that data could be taken from the database into cache in runtime?
Yes. A very simple implementation would be to have RetrieveVirtualItem to go out and fetch one item from the database and feed it to the ListView. That way, you would not be holding any data in memory yourself, and the ListView would hold whatever it needs, and no more. I am not saying this is the best approach, as it would cause a lot of small Database operations. There typically is an optimum somewhere in between; if you are willing to spend the effort, you could: - create a cache, keeping a limited number of items, using WeakReference to make them garbage collectable; - inside RetrieveVirtualItem, first look for the item in the cache; if found, pass it on. If not, read a number of items from the database, add them to the cache, and pass on the wanted one. That is why it is called virtual: the ListView will act as if it has all data, but in reality it only has part of the data at any one point in time, sufficient to show the visible parts. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
Ok But could we say that my virtualmode version was atleast 5% virtual :D Yes, the first thing that comes into my mind is that DB operation can be a bit slow because Database can also locate in local network. But this could be solved by taking several items with one search into cache. Second thing is sorting. There are two columns (alphanumeric strings) in my ListView and user must be able to sort both columns. Also in sorting itself there has been difficulties. I haven't find a way to sort mixed strings properly with sql query. jtpaa
-
Ok But could we say that my virtualmode version was atleast 5% virtual :D Yes, the first thing that comes into my mind is that DB operation can be a bit slow because Database can also locate in local network. But this could be solved by taking several items with one search into cache. Second thing is sorting. There are two columns (alphanumeric strings) in my ListView and user must be able to sort both columns. Also in sorting itself there has been difficulties. I haven't find a way to sort mixed strings properly with sql query. jtpaa
when a list is virtual, it can't sort as it does not hold all the data; sorting means either the list is not virtual, or you do the sorting, the list does the displaying. You can do sorting by including that in the SQL statement; see the ORDER BY clause. If you can and are willing to keep the data all in memory (and suffer a potential startup delay), there is no need for virtual mode. If not, you need to add something clever; using virtual mode would be a part of that; caching could also be; keeping one or two entire columns (for sorting) is also conceivable. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).